http请求无非就是ajax、src、href、表单
functionhookAJAX() { XMLHttpRequest.prototype.nativeOpen =XMLHttpRequest.prototype.open; var customizeOpen = function(method, url, async, user, password) { //do something this.nativeOpen(method, url, async, user, password); }; XMLHttpRequest.prototype.open =customizeOpen; } /** *全局拦截Image的图片请求添加token * */ functionhookImg() { const property = Object.getOwnPropertyDescriptor(Image.prototype, 'src'); const nativeSet =property.set; functioncustomiseSrcSet(url) { //do something nativeSet.call(this, url); } Object.defineProperty(Image.prototype, 'src', { set: customiseSrcSet, }); } /** * 拦截全局open的url添加token * */ functionhookOpen() { const nativeOpen =window.open; window.open = function(url) { //do something nativeOpen.call(this, url); }; } functionhookFetch() { var fet = Object.getOwnPropertyDescriptor(window, 'fetch') Object.defineProperty(window, 'fetch', { value: function(a, b, c) { //do something return fet.value.apply(this, args) } }) }