油猴 CORS 工具

在开发环境通过油猴提供 CORS 跨域

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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;
}