Skip to content

Commit f0c3a67

Browse files
committed
🐛 修复GM.xmlHttpRequest实现 #308
1 parent 50c7a36 commit f0c3a67

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptcat",
3-
"version": "0.16.5",
3+
"version": "0.16.6",
44
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
55
"author": "CodFrm",
66
"license": "GPLv3",

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "__MSG_scriptcat__",
4-
"version": "0.16.5",
4+
"version": "0.16.6",
55
"author": "CodFrm",
66
"description": "__MSG_scriptcat_description__",
77
"options_ui": {

src/runtime/content/gm_api.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,23 @@ export class GMContext {
3939
if (param.listener) {
4040
param.listener();
4141
}
42+
if (key === "GMdotXmlHttpRequest") {
43+
GMContext.apis.set("GM.xmlHttpRequest", {
44+
api: descriptor.value,
45+
param,
46+
});
47+
return;
48+
}
4249
GMContext.apis.set(key, {
4350
api: descriptor.value,
4451
param,
4552
});
4653
// 兼容GM.*
47-
let dot = key.replace("_", ".");
54+
const dot = key.replace("_", ".");
4855
if (dot !== key) {
4956
// 特殊处理GM.xmlHttpRequest
5057
if (dot === "GM.xmlhttpRequest") {
51-
dot = "GM.xmlHttpRequest";
58+
return;
5259
}
5360
GMContext.apis.set(dot, {
5461
api: descriptor.value,
@@ -263,6 +270,38 @@ export default class GMApi {
263270
return this.message.syncSend("CAT_createBlobUrl", blob);
264271
}
265272

273+
// 用于脚本跨域请求,需要@connect domain指定允许的域名
274+
@GMContext.API({
275+
depend: [
276+
"CAT_fetchBlob",
277+
"CAT_createBlobUrl",
278+
"CAT_fetchDocument",
279+
"GM_xmlhttpRequest",
280+
],
281+
})
282+
GMdotXmlHttpRequest(details: GMTypes.XHRDetails) {
283+
let abort: any;
284+
const ret = new Promise((resolve, reject) => {
285+
const oldOnload = details.onload;
286+
details.onload = (data) => {
287+
resolve(data);
288+
oldOnload && oldOnload(data);
289+
};
290+
const oldOnerror = details.onerror;
291+
details.onerror = (data) => {
292+
reject(data);
293+
oldOnerror && oldOnerror(data);
294+
};
295+
// @ts-ignore
296+
abort = this.GM_xmlhttpRequest(details);
297+
});
298+
if (abort && abort.abort) {
299+
// @ts-ignore
300+
ret.abort = abort.abort;
301+
}
302+
return ret;
303+
}
304+
266305
// 用于脚本跨域请求,需要@connect domain指定允许的域名
267306
@GMContext.API({
268307
depend: ["CAT_fetchBlob", "CAT_createBlobUrl", "CAT_fetchDocument"],

0 commit comments

Comments
 (0)