hookFetch

only hookFetch

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

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