gm-fetch

using fetch based on GM.xmlHttpRequest

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.greatest.deepsurf.us/scripts/483730/1305419/gm-fetch.js

  1. /**
  2. * Original file: /npm/@trim21/gm-fetch@0.1.15/dist/gm_fetch.js
  3. *
  4. * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
  5. */
  6. var GM_fetch = (function () {
  7. "use strict";
  8. function e(e) {
  9. const t = e.trim();
  10. if (!t) return new Headers();
  11. const r = t.split("\r\n").map((e) => {
  12. let t = e.split(":");
  13. return [t[0].trim(), t[1].trim()];
  14. });
  15. return new Headers(r);
  16. }
  17. class t {
  18. constructor(e, t) {
  19. var r;
  20. (this.rawBody = e),
  21. (this.init = t),
  22. (this.body =
  23. ((r = e),
  24. new ReadableStream({
  25. start(e) {
  26. e.enqueue(r), e.close();
  27. },
  28. })));
  29. const {
  30. headers: s,
  31. statusCode: o,
  32. statusText: n,
  33. finalUrl: a,
  34. redirected: d,
  35. } = t;
  36. (this.headers = s),
  37. (this.status = o),
  38. (this.statusText = n),
  39. (this.url = a),
  40. (this.type = "basic"),
  41. (this.redirected = d),
  42. (this._bodyUsed = !1);
  43. }
  44. get bodyUsed() {
  45. return this._bodyUsed;
  46. }
  47. get ok() {
  48. return this.status < 300;
  49. }
  50. arrayBuffer() {
  51. if (this.bodyUsed)
  52. throw new TypeError(
  53. "Failed to execute 'arrayBuffer' on 'Response': body stream already read"
  54. );
  55. return (this._bodyUsed = !0), this.rawBody.arrayBuffer();
  56. }
  57. blob() {
  58. if (this.bodyUsed)
  59. throw new TypeError(
  60. "Failed to execute 'blob' on 'Response': body stream already read"
  61. );
  62. return (
  63. (this._bodyUsed = !0),
  64. Promise.resolve(
  65. this.rawBody.slice(0, this.rawBody.size, this.rawBody.type)
  66. )
  67. );
  68. }
  69. clone() {
  70. if (this.bodyUsed)
  71. throw new TypeError(
  72. "Failed to execute 'clone' on 'Response': body stream already read"
  73. );
  74. return new t(this.rawBody, this.init);
  75. }
  76. formData() {
  77. if (this.bodyUsed)
  78. throw new TypeError(
  79. "Failed to execute 'formData' on 'Response': body stream already read"
  80. );
  81. return (this._bodyUsed = !0), this.rawBody.text().then(r);
  82. }
  83. async json() {
  84. if (this.bodyUsed)
  85. throw new TypeError(
  86. "Failed to execute 'json' on 'Response': body stream already read"
  87. );
  88. return (this._bodyUsed = !0), JSON.parse(await this.rawBody.text());
  89. }
  90. text() {
  91. if (this.bodyUsed)
  92. throw new TypeError(
  93. "Failed to execute 'text' on 'Response': body stream already read"
  94. );
  95. return (this._bodyUsed = !0), this.rawBody.text();
  96. }
  97. }
  98. function r(e) {
  99. const t = new FormData();
  100. return (
  101. e
  102. .trim()
  103. .split("&")
  104. .forEach(function (e) {
  105. if (e) {
  106. const r = e.split("="),
  107. s = r.shift()?.replace(/\+/g, " "),
  108. o = r.join("=").replace(/\+/g, " ");
  109. t.append(decodeURIComponent(s), decodeURIComponent(o));
  110. }
  111. }),
  112. t
  113. );
  114. }
  115. const s = [
  116. "GET",
  117. "POST",
  118. "PUT",
  119. "DELETE",
  120. "PATCH",
  121. "HEAD",
  122. "TRACE",
  123. "OPTIONS",
  124. "CONNECT",
  125. ];
  126. function o(e) {
  127. if (((t = e), s.includes(t))) return e;
  128. var t;
  129. throw new Error(`unsupported http method ${e}`);
  130. }
  131. return async function (r, s) {
  132. const n = new Request(r, s);
  133. let a;
  134. return (
  135. s?.body && (a = await n.text()),
  136. await (function (r, s, n) {
  137. return new Promise((a, d) => {
  138. if (r.signal && r.signal.aborted)
  139. return d(new DOMException("Aborted", "AbortError"));
  140. GM.xmlHttpRequest({
  141. url: r.url,
  142. method: o(r.method.toUpperCase()),
  143. headers: Object.fromEntries(new Headers(s?.headers).entries()),
  144. data: n,
  145. responseType: "blob",
  146. onload(s) {
  147. a(
  148. (function (r, s) {
  149. return new t(s.response, {
  150. statusCode: s.status,
  151. statusText: s.statusText,
  152. headers: e(s.responseHeaders),
  153. finalUrl: s.finalUrl,
  154. redirected: s.finalUrl === r.url,
  155. });
  156. })(r, s)
  157. );
  158. },
  159. onabort() {
  160. d(new DOMException("Aborted", "AbortError"));
  161. },
  162. ontimeout() {
  163. d(new TypeError("Network request failed, timeout"));
  164. },
  165. onerror(e) {
  166. d(new TypeError("Failed to fetch: " + e.finalUrl));
  167. },
  168. });
  169. });
  170. })(n, s, a)
  171. );
  172. };
  173. })();