From f0c3a6739e290426548d50209c241215ec005480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=80=E4=B9=8B?= Date: Thu, 24 Oct 2024 11:09:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8DGM.xmlHttpRequ?= =?UTF-8?q?est=E5=AE=9E=E7=8E=B0=20#308?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 4 ++-- package.json | 2 +- src/manifest.json | 2 +- src/runtime/content/gm_api.ts | 43 +++++++++++++++++++++++++++++++++-- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6d16d0ae..e246970a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "scriptcat", - "version": "0.16.5", + "version": "0.16.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "scriptcat", - "version": "0.16.5", + "version": "0.16.6", "license": "GPLv3", "dependencies": { "@arco-design/web-react": "^2.51.1", diff --git a/package.json b/package.json index e78002bb..115db18e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scriptcat", - "version": "0.16.5", + "version": "0.16.6", "description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!", "author": "CodFrm", "license": "GPLv3", diff --git a/src/manifest.json b/src/manifest.json index 5a6dbd1c..c7226404 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "__MSG_scriptcat__", - "version": "0.16.5", + "version": "0.16.6", "author": "CodFrm", "description": "__MSG_scriptcat_description__", "options_ui": { diff --git a/src/runtime/content/gm_api.ts b/src/runtime/content/gm_api.ts index 4a79b8e1..51b485b6 100644 --- a/src/runtime/content/gm_api.ts +++ b/src/runtime/content/gm_api.ts @@ -39,16 +39,23 @@ export class GMContext { if (param.listener) { param.listener(); } + if (key === "GMdotXmlHttpRequest") { + GMContext.apis.set("GM.xmlHttpRequest", { + api: descriptor.value, + param, + }); + return; + } GMContext.apis.set(key, { api: descriptor.value, param, }); // 兼容GM.* - let dot = key.replace("_", "."); + const dot = key.replace("_", "."); if (dot !== key) { // 特殊处理GM.xmlHttpRequest if (dot === "GM.xmlhttpRequest") { - dot = "GM.xmlHttpRequest"; + return; } GMContext.apis.set(dot, { api: descriptor.value, @@ -263,6 +270,38 @@ export default class GMApi { return this.message.syncSend("CAT_createBlobUrl", blob); } + // 用于脚本跨域请求,需要@connect domain指定允许的域名 + @GMContext.API({ + depend: [ + "CAT_fetchBlob", + "CAT_createBlobUrl", + "CAT_fetchDocument", + "GM_xmlhttpRequest", + ], + }) + GMdotXmlHttpRequest(details: GMTypes.XHRDetails) { + let abort: any; + const ret = new Promise((resolve, reject) => { + const oldOnload = details.onload; + details.onload = (data) => { + resolve(data); + oldOnload && oldOnload(data); + }; + const oldOnerror = details.onerror; + details.onerror = (data) => { + reject(data); + oldOnerror && oldOnerror(data); + }; + // @ts-ignore + abort = this.GM_xmlhttpRequest(details); + }); + if (abort && abort.abort) { + // @ts-ignore + ret.abort = abort.abort; + } + return ret; + } + // 用于脚本跨域请求,需要@connect domain指定允许的域名 @GMContext.API({ depend: ["CAT_fetchBlob", "CAT_createBlobUrl", "CAT_fetchDocument"],