gmfetch

A shim for using GM.xmlHttpRequest with a fetch-like API.

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/422172/903521/gmfetch.js

// ==UserScript==
// @name        gmfetch
// @author      matthin
// @namespace   https://github.com/matthin
// @version     1.0
// @license     GPL-3.0
// @description A shim for using GM.xmlHttpRequest with a fetch-like API.
// ==/UserScript==

function gmfetch(url) {
  return new Promise(resolve => {
    GM.xmlHttpRequest({
      url: url,
      onload: res => {
        const headers = new Headers();
        res.responseHeaders.trim().split('\n').forEach(line => {
          const [key, value] = line.split(': ');
          headers.append(key, value);
        });
        
        resolve(new Response(res.responseText, {
          status: res.status,
          statusText: res.statusText,
          headers: headers
        }));
      }
    });
  });
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址