Skip to content

Commit

Permalink
🐛 修复GM.xmlHttpRequest实现 #308
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Oct 24, 2024
1 parent 50c7a36 commit f0c3a67
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scriptcat",
"version": "0.16.5",
"version": "0.16.6",
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
"author": "CodFrm",
"license": "GPLv3",
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
43 changes: 41 additions & 2 deletions src/runtime/content/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"],
Expand Down

0 comments on commit f0c3a67

Please sign in to comment.