hookFetch

only hookFetch

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/465483/1186343/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. globalVariable.set('XMLHttpRequest', contextWindow.XMLHttpRequest);
  100. const RealXhr = contextWindow.XMLHttpRequest;
  101. class NewXhr extends RealXhr {
  102. send() {
  103. fetch(this._url, {
  104. method: this._method,
  105. headers: this._headers,
  106. body: this._body,
  107. credentials: this._withCredentials ? 'include' : 'omit'
  108. }).then((response) => {
  109. this.status = response.status;
  110. this.statusText = response.statusText;
  111. this.response = response.body;
  112. this.readyState = 4;
  113. this.dispatchEvent(new Event('readystatechange'));
  114. this.dispatchEvent(new Event('load'));
  115. this.dispatchEvent(new Event('loadend'));
  116. });
  117. }
  118. }
  119. contextWindow.XMLHttpRequest = NewXhr;
  120. function newXhr() {
  121. const xhr = new RealXhr();
  122. xhr._open = xhr.open;
  123. xhr._send = xhr.send;
  124. xhr.open = function (method, url, async = true) {
  125. this._method = method;
  126. this._url = url;
  127. this._async = async;
  128. this._headers = {};
  129. this._withCredentials = false;
  130. this._readyState = 0;
  131. this._responseType = '';
  132. };
  133. xhr.setRequestHeader = function (name, value) {
  134. this._headers[name] = value;
  135. };
  136. xhr.send = function (body) {
  137. this._body = body;
  138. this._send();
  139. };
  140.  
  141. return xhr;
  142. }
  143. return newXhr;
  144. }
  145.  
  146. (async () => {
  147. hookXhr();
  148. })();
  149.  
  150. (async () => {
  151. hookFetch();
  152. })();
  153.  
  154. contextWindow['__hookRequest__'] = {
  155. FetchCallback: {
  156. add: (pathname, callback) => {
  157. let list = FetchMapList.get(pathname) || (FetchMapList.set(pathname, []), FetchMapList.get(pathname));
  158. list.push(callback);
  159. let index = list.length;
  160. return index;
  161. },
  162. del: (pathname, index) => {
  163. try {
  164. let list = FetchMapList.get(pathname);
  165. if (list == null) return false;
  166. list.splice(index - 1, 1);
  167. } catch (e) {
  168. new Error(e);
  169. return false;
  170. }
  171. return true;
  172. }
  173. },
  174. XhrCallback: {
  175. add: (pathname, callback) => {
  176. let list = XhrMapList.get(pathname) || (XhrMapList.set(pathname, []), XhrMapList.get(pathname));
  177. list.push(callback);
  178. let index = list.length;
  179. return index;
  180. },
  181. del: (pathname, index) => {
  182. try {
  183. let list = XhrMapList.get(pathname);
  184. if (list == null) return false;
  185. list.splice(index - 1, 1);
  186. } catch (e) {
  187. new Error(e);
  188. return false;
  189. }
  190. return true;
  191. }
  192. },
  193. globalVariable: {
  194. get: (key) => {
  195. return globalVariable.get(key);
  196. },
  197. getAll: () => {
  198. return globalVariable.entries();
  199. },
  200. set: (key, value) => {
  201. globalVariable.set(key, value);
  202. },
  203. getOrDrfault: (key, defaultValue) => {
  204. return globalVariable.get(key) || defaultValue;
  205. }
  206. }
  207. };
  208. })();