Skip to content

Commit

Permalink
fix: gm_xhr默认不带上origin
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Dec 7, 2021
1 parent c15913c commit 70399b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/apps/grant/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export class BackgroundGrant {
let setCookie = '';
let cookie = '';
let anonymous = false;
let origin = '';
let isScriptcat = false;
let requestHeaders: chrome.webRequest.HttpHeader[] = [];
let unsafeHeader: { [key: string]: string } = {};
data.requestHeaders?.forEach((val, key) => {
Expand All @@ -74,6 +76,10 @@ export class BackgroundGrant {
anonymous = true;
break;
}
case "x-cat-" + this.rand + "-scriptcat": {
isScriptcat = true;
break;
}
case "x-cat-" + this.rand + "-host":
case "x-cat-" + this.rand + "-user-agent":
case "x-cat-" + this.rand + "-referer":
Expand All @@ -87,9 +93,12 @@ export class BackgroundGrant {
cookie = val.value || '';
break;
}
case "origin": {
origin = val.value || '';
break;
}
case "user-agent":
case "host":
case "origin":
case "referer":
case "accept-encoding":
case "connection":
Expand All @@ -102,6 +111,10 @@ export class BackgroundGrant {
}
}
});
// 不是脚本管理器,加上origin
if (!isScriptcat && origin) {
unsafeHeader['Origin'] = origin;
}
if (anonymous) {
cookie = '';
}
Expand Down Expand Up @@ -491,6 +504,7 @@ export class BackgroundGrant {
grant.data = { type: 'ontimeout', data: "" };
post.postMessage(grant);
}
xhr.setRequestHeader("X-Cat-" + this.rand + "-Scriptcat", "true");
for (let key in config.headers) {
const val = config.headers[key];
// 处理unsafe的header
Expand Down
8 changes: 8 additions & 0 deletions src/apps/grant/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ export class FrontendGrant implements ScriptContext {
@FrontendGrant.GMFunction({ depend: ['CAT_fetchBlob'] })
public async GM_xmlhttpRequest(details: GM_Types.XHRDetails) {
let u = new URL(details.url, window.location.href);
if (details.headers) {
for (const key in details.headers) {
if (key.toLowerCase() == 'cookie') {
details.cookie = details.cookie || details.headers[key];
delete details.headers[key];
}
}
}
let param: GM_Types.XHRDetails = {
method: details.method,
timeout: details.timeout,
Expand Down
2 changes: 1 addition & 1 deletion src/apps/script/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ export class ScriptManager extends Manager {
AppEvent.trigger(TabRemove, tabId);
});
chrome.tabs.onUpdated.addListener((tabId, info) => {
if (info.status === undefined || info.status == "unloaded") {
if (info.status == "unloaded") {
runMenu.delete(tabId);
AppEvent.trigger(TabRemove, tabId);
return;
Expand Down

0 comments on commit 70399b3

Please sign in to comment.