gm-fetch

using fetch based on GM.xmlHttpRequest

目前為 2024-01-03 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/483730/1305419/gm-fetch.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

/**
 * Original file: /npm/@trim21/[email protected]/dist/gm_fetch.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
var GM_fetch = (function () {
    "use strict";
    function e(e) {
      const t = e.trim();
      if (!t) return new Headers();
      const r = t.split("\r\n").map((e) => {
        let t = e.split(":");
        return [t[0].trim(), t[1].trim()];
      });
      return new Headers(r);
    }
    class t {
      constructor(e, t) {
        var r;
        (this.rawBody = e),
          (this.init = t),
          (this.body =
            ((r = e),
            new ReadableStream({
              start(e) {
                e.enqueue(r), e.close();
              },
            })));
        const {
          headers: s,
          statusCode: o,
          statusText: n,
          finalUrl: a,
          redirected: d,
        } = t;
        (this.headers = s),
          (this.status = o),
          (this.statusText = n),
          (this.url = a),
          (this.type = "basic"),
          (this.redirected = d),
          (this._bodyUsed = !1);
      }
      get bodyUsed() {
        return this._bodyUsed;
      }
      get ok() {
        return this.status < 300;
      }
      arrayBuffer() {
        if (this.bodyUsed)
          throw new TypeError(
            "Failed to execute 'arrayBuffer' on 'Response': body stream already read"
          );
        return (this._bodyUsed = !0), this.rawBody.arrayBuffer();
      }
      blob() {
        if (this.bodyUsed)
          throw new TypeError(
            "Failed to execute 'blob' on 'Response': body stream already read"
          );
        return (
          (this._bodyUsed = !0),
          Promise.resolve(
            this.rawBody.slice(0, this.rawBody.size, this.rawBody.type)
          )
        );
      }
      clone() {
        if (this.bodyUsed)
          throw new TypeError(
            "Failed to execute 'clone' on 'Response': body stream already read"
          );
        return new t(this.rawBody, this.init);
      }
      formData() {
        if (this.bodyUsed)
          throw new TypeError(
            "Failed to execute 'formData' on 'Response': body stream already read"
          );
        return (this._bodyUsed = !0), this.rawBody.text().then(r);
      }
      async json() {
        if (this.bodyUsed)
          throw new TypeError(
            "Failed to execute 'json' on 'Response': body stream already read"
          );
        return (this._bodyUsed = !0), JSON.parse(await this.rawBody.text());
      }
      text() {
        if (this.bodyUsed)
          throw new TypeError(
            "Failed to execute 'text' on 'Response': body stream already read"
          );
        return (this._bodyUsed = !0), this.rawBody.text();
      }
    }
    function r(e) {
      const t = new FormData();
      return (
        e
          .trim()
          .split("&")
          .forEach(function (e) {
            if (e) {
              const r = e.split("="),
                s = r.shift()?.replace(/\+/g, " "),
                o = r.join("=").replace(/\+/g, " ");
              t.append(decodeURIComponent(s), decodeURIComponent(o));
            }
          }),
        t
      );
    }
    const s = [
      "GET",
      "POST",
      "PUT",
      "DELETE",
      "PATCH",
      "HEAD",
      "TRACE",
      "OPTIONS",
      "CONNECT",
    ];
    function o(e) {
      if (((t = e), s.includes(t))) return e;
      var t;
      throw new Error(`unsupported http method ${e}`);
    }
    return async function (r, s) {
      const n = new Request(r, s);
      let a;
      return (
        s?.body && (a = await n.text()),
        await (function (r, s, n) {
          return new Promise((a, d) => {
            if (r.signal && r.signal.aborted)
              return d(new DOMException("Aborted", "AbortError"));
            GM.xmlHttpRequest({
              url: r.url,
              method: o(r.method.toUpperCase()),
              headers: Object.fromEntries(new Headers(s?.headers).entries()),
              data: n,
              responseType: "blob",
              onload(s) {
                a(
                  (function (r, s) {
                    return new t(s.response, {
                      statusCode: s.status,
                      statusText: s.statusText,
                      headers: e(s.responseHeaders),
                      finalUrl: s.finalUrl,
                      redirected: s.finalUrl === r.url,
                    });
                  })(r, s)
                );
              },
              onabort() {
                d(new DOMException("Aborted", "AbortError"));
              },
              ontimeout() {
                d(new TypeError("Network request failed, timeout"));
              },
              onerror(e) {
                d(new TypeError("Failed to fetch: " + e.finalUrl));
              },
            });
          });
        })(n, s, a)
      );
    };
  })();