Skip to content

Commit 70399b3

Browse files
committed
fix: gm_xhr默认不带上origin
1 parent c15913c commit 70399b3

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/apps/grant/background.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export class BackgroundGrant {
6161
let setCookie = '';
6262
let cookie = '';
6363
let anonymous = false;
64+
let origin = '';
65+
let isScriptcat = false;
6466
let requestHeaders: chrome.webRequest.HttpHeader[] = [];
6567
let unsafeHeader: { [key: string]: string } = {};
6668
data.requestHeaders?.forEach((val, key) => {
@@ -74,6 +76,10 @@ export class BackgroundGrant {
7476
anonymous = true;
7577
break;
7678
}
79+
case "x-cat-" + this.rand + "-scriptcat": {
80+
isScriptcat = true;
81+
break;
82+
}
7783
case "x-cat-" + this.rand + "-host":
7884
case "x-cat-" + this.rand + "-user-agent":
7985
case "x-cat-" + this.rand + "-referer":
@@ -87,9 +93,12 @@ export class BackgroundGrant {
8793
cookie = val.value || '';
8894
break;
8995
}
96+
case "origin": {
97+
origin = val.value || '';
98+
break;
99+
}
90100
case "user-agent":
91101
case "host":
92-
case "origin":
93102
case "referer":
94103
case "accept-encoding":
95104
case "connection":
@@ -102,6 +111,10 @@ export class BackgroundGrant {
102111
}
103112
}
104113
});
114+
// 不是脚本管理器,加上origin
115+
if (!isScriptcat && origin) {
116+
unsafeHeader['Origin'] = origin;
117+
}
105118
if (anonymous) {
106119
cookie = '';
107120
}
@@ -491,6 +504,7 @@ export class BackgroundGrant {
491504
grant.data = { type: 'ontimeout', data: "" };
492505
post.postMessage(grant);
493506
}
507+
xhr.setRequestHeader("X-Cat-" + this.rand + "-Scriptcat", "true");
494508
for (let key in config.headers) {
495509
const val = config.headers[key];
496510
// 处理unsafe的header

src/apps/grant/frontend.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ export class FrontendGrant implements ScriptContext {
135135
@FrontendGrant.GMFunction({ depend: ['CAT_fetchBlob'] })
136136
public async GM_xmlhttpRequest(details: GM_Types.XHRDetails) {
137137
let u = new URL(details.url, window.location.href);
138+
if (details.headers) {
139+
for (const key in details.headers) {
140+
if (key.toLowerCase() == 'cookie') {
141+
details.cookie = details.cookie || details.headers[key];
142+
delete details.headers[key];
143+
}
144+
}
145+
}
138146
let param: GM_Types.XHRDetails = {
139147
method: details.method,
140148
timeout: details.timeout,

src/apps/script/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ export class ScriptManager extends Manager {
765765
AppEvent.trigger(TabRemove, tabId);
766766
});
767767
chrome.tabs.onUpdated.addListener((tabId, info) => {
768-
if (info.status === undefined || info.status == "unloaded") {
768+
if (info.status == "unloaded") {
769769
runMenu.delete(tabId);
770770
AppEvent.trigger(TabRemove, tabId);
771771
return;

0 commit comments

Comments
 (0)