GM_CORS

CORS on test drive via GM_xmlhttpRequest

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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