Greasy Fork is available in English.

gmfetch

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

此脚本不应被直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://greasyfork.org/scripts/422172-gmfetch/code/gmfetch.js?version=903521

// ==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
        }));
      }
    });
  });
}