Greasy Fork is available in English.

hookFetch

only hookFetch

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greatest.deepsurf.us/scripts/465483/1186433/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. function deliveryTask(callbackList, _object, period) {
  9. let newObject = _object;
  10. for (let i = 0; i < callbackList.length; i++) {
  11. let tempObject = null;
  12. try {
  13. tempObject = callbackList[i](newObject, period);
  14. } catch (e) {
  15. new Error(e);
  16. }
  17. if (tempObject == null) {
  18. continue;
  19. }
  20. newObject = tempObject;
  21. }
  22. return newObject;
  23. }
  24.  
  25. function hookFetch() {
  26. const originalFetch = contextWindow.fetch;
  27. globalVariable.set('Fetch', originalFetch);
  28. contextWindow.fetch = (...args) => {
  29. let U = args[0];
  30. if (U.indexOf('http') == -1) {
  31. if (U[0] !== '/') {
  32. let pathname = new URL(location.href).pathname;
  33. U = pathname + U;
  34. }
  35. U = location.origin + U;
  36. }
  37. let apply = null;
  38. (() => {
  39. let url = new URL(U),
  40. pathname = url.pathname,
  41. callback = FetchMapList.get(pathname);
  42. if (callback == null) return;
  43. if (callback.length === 0) return;
  44. let newObject = deliveryTask(callback, { args }, 'preRequest');
  45. if (newObject && newObject.args) {
  46. args = newObject.args;
  47. }
  48. apply = originalFetch.apply(this, args);
  49. apply.then((response) => {
  50. let originalGetReader = response.body.getReader;
  51. response.body.getReader = function () {
  52. let originalReader = originalGetReader.apply(this, arguments);
  53. let originalRead = originalReader.read;
  54. originalReader.read = function () {
  55. return originalRead.apply(this, arguments).then(function (result) {
  56. let tempObject = deliveryTask(callback, { result, args }, 'doing');
  57. if (tempObject && tempObject.result) {
  58. result = tempObject.result;
  59. }
  60. return result;
  61. });
  62. };
  63. return originalReader;
  64. };
  65. let text = response.text,
  66. json = response.json;
  67. response.text = () => {
  68. return text.apply(response).then((text) => {
  69. let _object = deliveryTask(callback, { text, args }, 'done');
  70. if (_object && _object.text) {
  71. text = _object.text;
  72. }
  73. return text;
  74. });
  75. };
  76. response.json = () => {
  77. return json.apply(response).then((json) => {
  78. let text = JSON.stringify(json);
  79. let _object = deliveryTask(callback, { text, args }, 'done');
  80. if (_object && _object.text) {
  81. text = _object.text;
  82. return JSON.parse(text);
  83. }
  84. return json;
  85. });
  86. };
  87. });
  88. })();
  89. if (apply == null) {
  90. apply = originalFetch.apply(this, args);
  91. }
  92. return apply;
  93. };
  94. }
  95.  
  96. hookFetch();
  97.  
  98. contextWindow['__hookRequest__'] = {
  99. FetchCallback: {
  100. add: (pathname, callback) => {
  101. let list = FetchMapList.get(pathname) || (FetchMapList.set(pathname, []), FetchMapList.get(pathname));
  102. list.push(callback);
  103. let index = list.length;
  104. return index;
  105. },
  106. del: (pathname, index) => {
  107. try {
  108. let list = FetchMapList.get(pathname);
  109. if (list == null) return false;
  110. list.splice(index - 1, 1);
  111. } catch (e) {
  112. new Error(e);
  113. return false;
  114. }
  115. return true;
  116. }
  117. },
  118. globalVariable: {
  119. get: (key) => {
  120. return globalVariable.get(key);
  121. },
  122. getAll: () => {
  123. return globalVariable.entries();
  124. },
  125. set: (key, value) => {
  126. globalVariable.set(key, value);
  127. },
  128. getOrDrfault: (key, defaultValue) => {
  129. return globalVariable.get(key) || defaultValue;
  130. }
  131. }
  132. };
  133. })();