hookFetch

only hookFetch

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/465483/1186369/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. function hookXhr() {
  99. const oldXHR = contextWindow.XMLHttpRequest;
  100. globalVariable.set('XMLHttpRequest', oldXHR);
  101. const XHRProxy = new Proxy(contextWindow.XMLHttpRequest, {
  102. construct(target, args) {
  103. let xhr = new target(...args);
  104. let originalOpen = xhr.open;
  105. let originalSend = xhr.send;
  106. let url = '';
  107. xhr.open = function () {
  108. url = arguments[1];
  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 == '' ? url : 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. if (xhr.responseText !== newObject.text) {
  140. debugger;
  141. }
  142. }
  143. }
  144. onReadyStateChangeOriginal && onReadyStateChangeOriginal.apply(xhr, args);
  145. };
  146. return o(args);
  147. };
  148. return xhr;
  149. }
  150. });
  151. contextWindow.XMLHttpRequest = XHRProxy;
  152. }
  153.  
  154. (async () => {
  155. hookFetch();
  156. })();
  157. (async () => {
  158. hookXhr();
  159. })();
  160.  
  161. contextWindow['__hookRequest__'] = {
  162. FetchCallback: {
  163. add: (pathname, callback) => {
  164. let list = FetchMapList.get(pathname) || (FetchMapList.set(pathname, []), FetchMapList.get(pathname));
  165. list.push(callback);
  166. let index = list.length;
  167. return index;
  168. },
  169. del: (pathname, index) => {
  170. try {
  171. let list = FetchMapList.get(pathname);
  172. if (list == null) return false;
  173. list.splice(index - 1, 1);
  174. } catch (e) {
  175. new Error(e);
  176. return false;
  177. }
  178. return true;
  179. }
  180. },
  181. XhrCallback: {
  182. add: (pathname, callback) => {
  183. let list = XhrMapList.get(pathname) || (XhrMapList.set(pathname, []), XhrMapList.get(pathname));
  184. list.push(callback);
  185. let index = list.length;
  186. return index;
  187. },
  188. del: (pathname, index) => {
  189. try {
  190. let list = XhrMapList.get(pathname);
  191. if (list == null) return false;
  192. list.splice(index - 1, 1);
  193. } catch (e) {
  194. new Error(e);
  195. return false;
  196. }
  197. return true;
  198. }
  199. },
  200. globalVariable: {
  201. get: (key) => {
  202. return globalVariable.get(key);
  203. },
  204. getAll: () => {
  205. return globalVariable.entries();
  206. },
  207. set: (key, value) => {
  208. globalVariable.set(key, value);
  209. },
  210. getOrDrfault: (key, defaultValue) => {
  211. return globalVariable.get(key) || defaultValue;
  212. }
  213. }
  214. };
  215. })();