此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.cloud/scripts/465483/1186357/hookFetch.js
您需要先安装一款用户样式管理器扩展(如 Stylus)后才能安装此样式。
您需要先安装一款用户样式管理器扩展(如 Stylus)后才能安装此样式。
您需要先安装一款用户样式管理器扩展(如 Stylus)后才能安装此样式。
您需要先安装一款用户样式管理器扩展后才能安装此样式。
您需要先安装一款用户样式管理器扩展后才能安装此样式。
您需要先安装一款用户样式管理器扩展后才能安装此样式。
(我已经安装了用户样式管理器,让我安装!)
(() => {
var contextWindow = window.unsafeWindow || document.defaultView || window;
if (contextWindow['__hookRequest__'] != null) {
return;
}
var globalVariable = new Map();
var FetchMapList = new Map();
var XhrMapList = new Map();
function deliveryTask(callbackList, _object, period) {
let newObject = _object;
for (let i = 0; i < callbackList.length; i++) {
let tempObject = null;
try {
tempObject = callbackList[i](newObject, period);
} catch (e) {
new Error(e);
}
if (tempObject == null) {
continue;
}
newObject = tempObject;
}
return newObject;
}
function hookFetch() {
const originalFetch = contextWindow.fetch;
globalVariable.set('Fetch', originalFetch);
contextWindow.fetch = (...args) => {
let U = args[0];
if (U.indexOf('http') == -1) {
if (U[0] !== '/') {
let pathname = new URL(location.href).pathname;
U = pathname + U;
}
U = location.origin + U;
}
let apply = null;
(() => {
let url = new URL(U),
pathname = url.pathname,
callback = FetchMapList.get(pathname);
if (callback == null) return;
if (callback.length === 0) return;
let newObject = deliveryTask(callback, { args }, 'preRequest');
if (newObject && newObject.args) {
args = newObject.args;
}
apply = originalFetch.apply(this, args);
apply.then((response) => {
let originalGetReader = response.body.getReader;
response.body.getReader = function () {
let originalReader = originalGetReader.apply(this, arguments);
let originalRead = originalReader.read;
originalReader.read = function () {
return originalRead.apply(this, arguments).then(function (result) {
let tempObject = deliveryTask(callback, { result, args }, 'doing');
if (tempObject && tempObject.result) {
result = tempObject.result;
}
return result;
});
};
return originalReader;
};
let text = response.text,
json = response.json;
response.text = () => {
return text.apply(response).then((text) => {
let _object = deliveryTask(callback, { text, args }, 'done');
if (_object && _object.text) {
text = _object.text;
}
return text;
});
};
response.json = () => {
return json.apply(response).then((json) => {
let text = JSON.stringify(json);
let _object = deliveryTask(callback, { text, args }, 'done');
if (_object && _object.text) {
text = _object.text;
return JSON.parse(text);
}
return json;
});
};
});
})();
if (apply == null) {
apply = originalFetch.apply(this, args);
}
return apply;
};
}
// globalVariable.set('XMLHttpRequest', contextWindow.XMLHttpRequest);
function hookXhr() {
// 保存原生 XMLHttpRequest 对象的 open 方法和 send 方法
const originalOpen = XMLHttpRequest.prototype.open;
const originalSend = XMLHttpRequest.prototype.send;
// 修改 XMLHttpRequest 对象的 open 方法
XMLHttpRequest.prototype.open = function (method, url) {
this._url = url;
this._responseType = null;
originalOpen.apply(this, arguments);
};
// 修改 XMLHttpRequest 对象的 send 方法
XMLHttpRequest.prototype.send = function () {
const self = this;
// 保存原生 XMLHttpRequest 对象的 onreadystatechange 方法
const originalReadyStateChange = this.onreadystatechange;
// 修改 XMLHttpRequest 对象的 onreadystatechange 方法
this.onreadystatechange = function () {
if (self.readyState === 4 && self.status === 200) {
// 修改 responseText
const modifiedResponseText = 'Modified ' + self.responseText;
self.responseText = modifiedResponseText;
}
// 调用原生 XMLHttpRequest 对象的 onreadystatechange 方法
if (typeof originalReadyStateChange === 'function') {
originalReadyStateChange.apply(self, arguments);
}
};
// 设置 responseType 属性
if (this._responseType) {
this.responseType = this._responseType;
}
// 调用原生 XMLHttpRequest 对象的 send 方法
originalSend.apply(this, arguments);
};
// 新增设置 responseType 的方法
XMLHttpRequest.prototype.setResponseType = function (responseType) {
this._responseType = responseType;
};
}
hookFetch();
hookXhr();
contextWindow['__hookRequest__'] = {
FetchCallback: {
add: (pathname, callback) => {
let list = FetchMapList.get(pathname) || (FetchMapList.set(pathname, []), FetchMapList.get(pathname));
list.push(callback);
let index = list.length;
return index;
},
del: (pathname, index) => {
try {
let list = FetchMapList.get(pathname);
if (list == null) return false;
list.splice(index - 1, 1);
} catch (e) {
new Error(e);
return false;
}
return true;
}
},
XhrCallback: {
add: (pathname, callback) => {
let list = XhrMapList.get(pathname) || (XhrMapList.set(pathname, []), XhrMapList.get(pathname));
list.push(callback);
let index = list.length;
return index;
},
del: (pathname, index) => {
try {
let list = XhrMapList.get(pathname);
if (list == null) return false;
list.splice(index - 1, 1);
} catch (e) {
new Error(e);
return false;
}
return true;
}
},
globalVariable: {
get: (key) => {
return globalVariable.get(key);
},
getAll: () => {
return globalVariable.entries();
},
set: (key, value) => {
globalVariable.set(key, value);
},
getOrDrfault: (key, defaultValue) => {
return globalVariable.get(key) || defaultValue;
}
}
};
})();