hookFetch

only hookFetch

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

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

QingJ © 2025

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