hookFetch

only hookFetch

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/465483/1186013/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. if (result.done) {
  59. return result;
  60. } else {
  61. let tempObject = deliveryTask(callback, { binary: result.value, args }, 'doing');
  62. if (tempObject && tempObject.binary) {
  63. result.value = tempObject.binary;
  64. }
  65. return result;
  66. }
  67. });
  68. };
  69. return originalReader;
  70. };
  71. let text = response.text,
  72. json = response.json;
  73. response.text = () => {
  74. return text.apply(response).then((text) => {
  75. let _object = deliveryTask(callback, { text, args }, 'done');
  76. if (_object && _object.text) {
  77. text = _object.text;
  78. }
  79. return text;
  80. });
  81. };
  82. response.json = () => {
  83. return json.apply(response).then((json) => {
  84. let text = JSON.stringify(json);
  85. let _object = deliveryTask(callback, { text, args }, 'done');
  86. if (_object && _object.text) {
  87. text = _object.text;
  88. return JSON.parse(text);
  89. }
  90. return json;
  91. });
  92. };
  93. });
  94. })();
  95. if (apply == null) {
  96. apply = originalFetch.apply(this, args);
  97. }
  98. return apply;
  99. };
  100. }
  101.  
  102. function hookXhr() {
  103. const XHRProxy = new Proxy(contextWindow.XMLHttpRequest, {
  104. construct(target, args) {
  105. const xhr = new target(...args);
  106. const originalOpen = xhr.open;
  107. const originalSend = xhr.send;
  108. xhr.open = function () {
  109. return originalOpen.apply(xhr, arguments);
  110. };
  111. xhr.send = function () {
  112. let o = function (args) {
  113. return originalSend.apply(xhr, args);
  114. };
  115. let args = arguments;
  116. let U = xhr.responseURL;
  117. if (U.indexOf('http') == -1) {
  118. if (U[0] !== '/') {
  119. let pathname = new URL(location.href).pathname;
  120. U = pathname + U;
  121. }
  122. U = location.origin + U;
  123. }
  124. let pathname = new URL(U).pathname;
  125. let callback = XhrMapList.get(pathname);
  126. if (callback == null) return o(args);
  127. if (callback.length === 0) return o(args);
  128. let newObject = deliveryTask(callback, { args }, 'preRequest');
  129. if (newObject && newObject.args) {
  130. args = newObject.args;
  131. }
  132. const onReadyStateChangeOriginal = xhr.onreadystatechange;
  133. xhr.onreadystatechange = function () {
  134. if (xhr.readyState === 4 && xhr.status === 200) {
  135. let text = xhr.responseText;
  136. let newObject = deliveryTask(callback, { text, args }, 'done');
  137. if (newObject && newObject.text) {
  138. xhr.responseText = newObject.text;
  139. }
  140. }
  141. onReadyStateChangeOriginal && onReadyStateChangeOriginal.apply(xhr, args);
  142. };
  143. return o(args);
  144. };
  145. return xhr;
  146. }
  147. });
  148. globalVariable.set('XMLHttpRequest', contextWindow.XMLHttpRequest);
  149. contextWindow.XMLHttpRequest = XHRProxy;
  150. }
  151.  
  152. (async () => {
  153. hookFetch();
  154. })();
  155. (async () => {
  156. hookXhr();
  157. })();
  158.  
  159. contextWindow['__hookRequest__'] = {
  160. FetchCallback: {
  161. add: (pathname, callback) => {
  162. let list = FetchMapList.get(pathname) || (FetchMapList.set(pathname, []), FetchMapList.get(pathname));
  163. list.push(callback);
  164. let index = list.length;
  165. return index;
  166. },
  167. del: (pathname, index) => {
  168. try {
  169. let list = FetchMapList.get(pathname);
  170. if (list == null) return false;
  171. list.splice(index - 1, 1);
  172. } catch (e) {
  173. new Error(e);
  174. return false;
  175. }
  176. return true;
  177. }
  178. },
  179. XhrCallback: {
  180. add: (pathname, callback) => {
  181. let list = XhrMapList.get(pathname) || (XhrMapList.set(pathname, []), XhrMapList.get(pathname));
  182. list.push(callback);
  183. let index = list.length;
  184. return index;
  185. },
  186. del: (pathname, index) => {
  187. try {
  188. let list = XhrMapList.get(pathname);
  189. if (list == null) return false;
  190. list.splice(index - 1, 1);
  191. } catch (e) {
  192. new Error(e);
  193. return false;
  194. }
  195. return true;
  196. }
  197. },
  198. globalVariable: {
  199. get: (key) => {
  200. return globalVariable.get(key);
  201. },
  202. getAll: () => {
  203. return globalVariable.entries();
  204. },
  205. set: (key, value) => {
  206. globalVariable.set(key, value);
  207. },
  208. getOrDrfault: (key, defaultValue) => {
  209. return globalVariable.get(key) || defaultValue;
  210. }
  211. }
  212. };
  213. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址