GM_CORS

CORS on test drive via GM_xmlhttpRequest

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name              GM_CORS
// @name:zh-CN        油猴 CORS 工具
// @description       CORS on test drive via GM_xmlhttpRequest
// @description:zh-CN 在开发环境通过油猴提供 CORS 跨域
// @version           1.04
// @include           /^(file:///.*/index.html|https?://(127.0.0.1|192.168.\d+.\d+|localhost)(:\d+)?/(([^/]*/)?bundled/)?(index.html)?)(\?.*|$)/
// @include           /\b(pages\.dev|onrender\.com)\b/
// @include           /\b(?:github.io|gitlab.io|glitch.me|js.org|eu.org|web.app|netlify.(?:com|app)|now.sh|vercel.(?:com|app)|herokuapp.com|neocities.org|caitou.org)/
// @connect           *
// @require           https://unpkg.com/@trim21/[email protected]/dist/gm_fetch.js
// @grant             GM_xmlhttpRequest
// @run-at            document-start
// @license           WTFPL
// @namespace         https://greasyfork.org/users/448274
// ==/UserScript==

const GmCors = document.body.appendChild(Object.assign(document.createElement('div'), { id: 'GmCors' }))

GmCors.init = function (window) {
  if (!window) throw 'No window specified.'
  window.fetch = GM_fetch
  window.xhr = req => new Promise((resolve, reject) => GM_xmlhttpRequest({
    ...req,
    onload: response => resolve(response),
    onerror: error => reject(error)
  }))

  // for sw
  window.GM_CORS = new BroadcastChannel('GM_CORS')
  window.GM_CORS.onmessage = async e => {
    const { reqType, req } = e
    if (reqType === 'fetch') {
      const response = await GM_fetch(req)
      window.GM_CORS.postMessage(response)
    } else if (reqType === 'xhr') {
      GM_xmlhttpRequest({
        ...req,
         onload: response => window.GM_CORS.postMessage(response)
      })
    }
  }

  window.GmCors = { fetch: window.fetch, xhr: window.xhr };
  console.info('GmCors activated.');
  return window.GmCors;
}