Skip to content

Commit

Permalink
✨ GM_xhr 支持maxRedirects
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 18, 2022
1 parent b218cf4 commit 4d29cae
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
68 changes: 45 additions & 23 deletions src/apps/grant/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,26 @@ export class BackgroundGrant {
reqOpt.push('extraHeaders');
respOpt.push('extraHeaders');
}
chrome.webRequest.onBeforeSendHeaders.addListener((data) => {
const maxRedirects = new Map<string, [number, number]>();
// 处理重定向请求
chrome.webRequest.onBeforeSendHeaders.addListener((details) => {
if (!this.isExtensionRequest(details)) {
return;
}
let setCookie = '';
let cookie = '';
let anonymous = false;
let origin = '';
let isScriptcat = false;
const requestHeaders: chrome.webRequest.HttpHeader[] = [];
const unsafeHeader: { [key: string]: string } = {};
data.requestHeaders?.forEach((val) => {
details.requestHeaders?.forEach((val) => {
const lowerCase = val.name.toLowerCase();
switch (lowerCase) {
case 'x-cat-' + this.rand + '-max-redirects': {
maxRedirects.set(details.requestId, [0, parseInt(val.value || '')]);
break;
}
case 'x-cat-' + this.rand + '-cookie': {
setCookie = val.value || '';
break;
Expand Down Expand Up @@ -166,16 +175,20 @@ export class BackgroundGrant {
urls: ['<all_urls>'],
}, reqOpt);
const responseHeader: { [key: string]: boolean } = { 'set-cookie': true };
chrome.webRequest.onHeadersReceived.addListener((details: chrome.webRequest.WebResponseHeadersDetails & { originUrl?: string }) => {
if ((details.initiator && chrome.extension.getURL('').startsWith(details.initiator)) ||
(details.originUrl && details.originUrl.startsWith(chrome.extension.getURL('')))) {
details.responseHeaders?.forEach(val => {
chrome.webRequest.onHeadersReceived.addListener((details) => {
if (this.isExtensionRequest(details)) {
details.responseHeaders?.forEach((val) => {
if (responseHeader[val.name]) {
details.responseHeaders?.push({
name: 'x-cat-' + this.rand + '-' + val.name,
value: val.value,
binaryValue: val.binaryValue,
});
val.name = 'x-cat-' + this.rand + '-' + val.name;
}
if (val.name.toLowerCase() === 'location') {
const nums = maxRedirects.get(details.requestId);
if (nums) {
nums[0]++
if (nums[0] > nums[1]) {
val.name = 'x-cat-' + this.rand + '-' + val.name;
}
}
}
});
return {
Expand All @@ -185,11 +198,22 @@ export class BackgroundGrant {
}, {
urls: ['<all_urls>'],
}, respOpt);
chrome.webRequest.onCompleted.addListener((details) => {
if (!this.isExtensionRequest(details)) {
return;
}
maxRedirects.delete(details.requestId);
}, { urls: ['<all_urls>'] });
} catch (e) {
console.log(e);
}
}

protected isExtensionRequest(details: chrome.webRequest.ResourceRequest & { originUrl?: string }): boolean {
return (details.initiator && chrome.extension.getURL('').startsWith(details.initiator)) ||
(details.originUrl && details.originUrl.startsWith(chrome.extension.getURL(''))) ? true : false;
}

// 单实例
public static SingleInstance(scriptMgr: ScriptManager, listener: IGrantListener, isdebug: boolean): BackgroundGrant {
if (!BackgroundGrant._singleInstance) {
Expand Down Expand Up @@ -573,8 +597,7 @@ export class BackgroundGrant {
post.postMessage(grant);
}

this.dealUnsafeHeader(xhr, config.headers);
this.dealXhrCookie(xhr, config.cookie, config.anonymous);
this.dealUnsafeHeader(config, xhr, config.headers);

if (config.timeout) {
xhr.timeout = config.timeout;
Expand Down Expand Up @@ -1284,7 +1307,7 @@ export class BackgroundGrant {
});
}

protected dealUnsafeHeader(xhr: XMLHttpRequest, headers?: { [key: string]: string }): { [key: string]: string } {
protected dealUnsafeHeader(config: GMSend.XHRDetails, xhr: XMLHttpRequest, headers?: { [key: string]: string }): { [key: string]: string } {
xhr.setRequestHeader('X-Cat-' + this.rand + '-Scriptcat', 'true');
for (let key in headers) {
const val = headers[key];
Expand All @@ -1306,16 +1329,16 @@ export class BackgroundGrant {
App.Log.Debug('gmxhr', (e as Error).message, 'GM_xmlhttpRequest');
}
}
return headers || {};
}

protected dealXhrCookie(xhr: XMLHttpRequest, cookie?: string, anonymous?: boolean) {
if (cookie) {
xhr.setRequestHeader('X-Cat-' + this.rand + '-Cookie', cookie);
if (config.maxRedirects !== undefined) {
xhr.setRequestHeader('X-Cat-' + this.rand + '-Max-redirects', config.maxRedirects.toString());
}
if (anonymous) {
if (config.cookie) {
xhr.setRequestHeader('X-Cat-' + this.rand + '-Cookie', config.cookie);
}
if (config.anonymous) {
xhr.setRequestHeader('X-Cat-' + this.rand + '-Anonymous', 'true');
}
return headers || {};
}

@BackgroundGrant.GMFunction()
Expand Down Expand Up @@ -1384,8 +1407,7 @@ export class BackgroundGrant {
post.postMessage(grant);
}

this.dealUnsafeHeader(xhr, config.headers);
this.dealXhrCookie(xhr, config.cookie, config.anonymous);
this.dealUnsafeHeader(config, xhr, config.headers);

if (config.timeout) {
xhr.timeout = config.timeout;
Expand Down
3 changes: 2 additions & 1 deletion src/apps/grant/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export class FrontendGrant implements ScriptContext {
overrideMimeType: details.overrideMimeType,
anonymous: details.anonymous,
user: details.user,
password: details.password
password: details.password,
maxRedirects: details.maxRedirects,
};
if (!param.headers) {
param.headers = {};
Expand Down
1 change: 1 addition & 0 deletions src/types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ declare namespace GMSend {
password?: string,
nocache?: boolean
dataType?: 'FormData' | 'Blob'
maxRedirects?: number
}

interface XHRFormData {
Expand Down
1 change: 1 addition & 0 deletions src/types/scriptcat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ declare namespace GM_Types {
user?: string,
password?: string,
nocache?: boolean
maxRedirects?: number

onload?: Listener<XHRResponse>,
onloadstart?: Listener<XHRResponse>,
Expand Down

0 comments on commit 4d29cae

Please sign in to comment.