hookFetch

only hookFetch

当前为 2023-05-06 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/465483/1186358/hookFetch.js

  1. (() => {
  2. var contextWindow = window.unsafeWindow || document.defaultView || window;
  3. if (contextWindow['__hookRequest__'] != null) {
  4. return;
  5. }
  6. var globalVariable = new Map();
  7. var FetchMapList = new Map();
  8. var XhrMapList = new Map();
  9.  
  10. function deliveryTask(callbackList, _object, period) {
  11. let newObject = _object;
  12. for (let i = 0; i < callbackList.length; i++) {
  13. let tempObject = null;
  14. try {
  15. tempObject = callbackList[i](newObject, period);
  16. } catch (e) {
  17. new Error(e);
  18. }
  19. if (tempObject == null) {
  20. continue;
  21. }
  22. newObject = tempObject;
  23. }
  24. return newObject;
  25. }
  26.  
  27. function hookFetch() {
  28. const originalFetch = contextWindow.fetch;
  29. globalVariable.set('Fetch', originalFetch);
  30. contextWindow.fetch = (...args) => {
  31. let U = args[0];
  32. if (U.indexOf('http') == -1) {
  33. if (U[0] !== '/') {
  34. let pathname = new URL(location.href).pathname;
  35. U = pathname + U;
  36. }
  37. U = location.origin + U;
  38. }
  39. let apply = null;
  40. (() => {
  41. let url = new URL(U),
  42. pathname = url.pathname,
  43. callback = FetchMapList.get(pathname);
  44. if (callback == null) return;
  45. if (callback.length === 0) return;
  46. let newObject = deliveryTask(callback, { args }, 'preRequest');
  47. if (newObject && newObject.args) {
  48. args = newObject.args;
  49. }
  50. apply = originalFetch.apply(this, args);
  51. apply.then((response) => {
  52. let originalGetReader = response.body.getReader;
  53. response.body.getReader = function () {
  54. let originalReader = originalGetReader.apply(this, arguments);
  55. let originalRead = originalReader.read;
  56. originalReader.read = function () {
  57. return originalRead.apply(this, arguments).then(function (result) {
  58. let tempObject = deliveryTask(callback, { result, args }, 'doing');
  59. if (tempObject && tempObject.result) {
  60. result = tempObject.result;
  61. }
  62. return result;
  63. });
  64. };
  65. return originalReader;
  66. };
  67. let text = response.text,
  68. json = response.json;
  69. response.text = () => {
  70. return text.apply(response).then((text) => {
  71. let _object = deliveryTask(callback, { text, args }, 'done');
  72. if (_object && _object.text) {
  73. text = _object.text;
  74. }
  75. return text;
  76. });
  77. };
  78. response.json = () => {
  79. return json.apply(response).then((json) => {
  80. let text = JSON.stringify(json);
  81. let _object = deliveryTask(callback, { text, args }, 'done');
  82. if (_object && _object.text) {
  83. text = _object.text;
  84. return JSON.parse(text);
  85. }
  86. return json;
  87. });
  88. };
  89. });
  90. })();
  91. if (apply == null) {
  92. apply = originalFetch.apply(this, args);
  93. }
  94. return apply;
  95. };
  96. }
  97.  
  98. // globalVariable.set('XMLHttpRequest', contextWindow.XMLHttpRequest);
  99.  
  100. function hookXhr() {
  101. // 保存原生 XMLHttpRequest 对象的 open 方法和 send 方法
  102. const originalOpen = contextWindow.XMLHttpRequest.prototype.open;
  103. const originalSend = contextWindow.XMLHttpRequest.prototype.send;
  104.  
  105. // 修改 XMLHttpRequest 对象的 open 方法
  106. contextWindow.XMLHttpRequest.prototype.open = function (method, url) {
  107. this._url = url;
  108. this._responseType = null;
  109. originalOpen.apply(this, arguments);
  110. };
  111.  
  112. // 修改 XMLHttpRequest 对象的 send 方法
  113. contextWindow.XMLHttpRequest.prototype.send = function () {
  114. const self = this;
  115.  
  116. // 保存原生 XMLHttpRequest 对象的 onreadystatechange 方法
  117. const originalReadyStateChange = this.onreadystatechange;
  118.  
  119. // 修改 XMLHttpRequest 对象的 onreadystatechange 方法
  120. this.onreadystatechange = function () {
  121. if (self.readyState === 4 && self.status === 200) {
  122. // 修改 responseText
  123. const modifiedResponseText = 'Modified ' + self.responseText;
  124. self.responseText = modifiedResponseText;
  125. }
  126.  
  127. // 调用原生 XMLHttpRequest 对象的 onreadystatechange 方法
  128. if (typeof originalReadyStateChange === 'function') {
  129. originalReadyStateChange.apply(self, arguments);
  130. }
  131. };
  132.  
  133. // 设置 responseType 属性
  134. if (this._responseType) {
  135. this.responseType = this._responseType;
  136. }
  137.  
  138. // 调用原生 XMLHttpRequest 对象的 send 方法
  139. originalSend.apply(this, arguments);
  140. };
  141.  
  142. // 新增设置 responseType 的方法
  143. contextWindow.XMLHttpRequest.prototype.setResponseType = function (responseType) {
  144. this._responseType = responseType;
  145. };
  146. }
  147.  
  148. hookFetch();
  149. hookXhr();
  150.  
  151. contextWindow['__hookRequest__'] = {
  152. FetchCallback: {
  153. add: (pathname, callback) => {
  154. let list = FetchMapList.get(pathname) || (FetchMapList.set(pathname, []), FetchMapList.get(pathname));
  155. list.push(callback);
  156. let index = list.length;
  157. return index;
  158. },
  159. del: (pathname, index) => {
  160. try {
  161. let list = FetchMapList.get(pathname);
  162. if (list == null) return false;
  163. list.splice(index - 1, 1);
  164. } catch (e) {
  165. new Error(e);
  166. return false;
  167. }
  168. return true;
  169. }
  170. },
  171. XhrCallback: {
  172. add: (pathname, callback) => {
  173. let list = XhrMapList.get(pathname) || (XhrMapList.set(pathname, []), XhrMapList.get(pathname));
  174. list.push(callback);
  175. let index = list.length;
  176. return index;
  177. },
  178. del: (pathname, index) => {
  179. try {
  180. let list = XhrMapList.get(pathname);
  181. if (list == null) return false;
  182. list.splice(index - 1, 1);
  183. } catch (e) {
  184. new Error(e);
  185. return false;
  186. }
  187. return true;
  188. }
  189. },
  190. globalVariable: {
  191. get: (key) => {
  192. return globalVariable.get(key);
  193. },
  194. getAll: () => {
  195. return globalVariable.entries();
  196. },
  197. set: (key, value) => {
  198. globalVariable.set(key, value);
  199. },
  200. getOrDrfault: (key, defaultValue) => {
  201. return globalVariable.get(key) || defaultValue;
  202. }
  203. }
  204. };
  205. })();