Skip to content

Commit 53bac0b

Browse files
committed
feat: GM_XHR支持返回set-cookie & 处理xhr.response兼容问题
1 parent a0eabad commit 53bac0b

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

src/apps/grant/background.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,26 @@ export class BackgroundGrant {
126126
}, {
127127
urls: ["<all_urls>"],
128128
}, ["blocking", "requestHeaders", "extraHeaders"]);
129+
let responseHeader: { [key: string]: boolean } = { "set-cookie": true };
130+
chrome.webRequest.onHeadersReceived.addListener((details) => {
131+
if (details.initiator && chrome.extension.getURL("").startsWith(details.initiator)) {
132+
details.responseHeaders?.forEach(val => {
133+
if (responseHeader[val.name]) {
134+
details.responseHeaders?.push({
135+
name: "x-cat-" + this.rand + "-" + val.name,
136+
value: val.value,
137+
binaryValue: val.binaryValue,
138+
});
139+
}
140+
});
141+
console.log(details.responseHeaders);
142+
return {
143+
responseHeaders: details.responseHeaders
144+
}
145+
}
146+
}, {
147+
urls: ["<all_urls>"],
148+
}, ["blocking", "responseHeaders", "extraHeaders"]);
129149
} catch (e) {
130150
}
131151
}
@@ -350,41 +370,38 @@ export class BackgroundGrant {
350370

351371
}
352372

353-
354373
protected dealXhr(config: GM_Types.XHRDetails, xhr: XMLHttpRequest): GM_Types.XHRResponse {
374+
let removeXCat = new RegExp("x-cat-" + this.rand + "-", "g");
375+
console.log(xhr.getAllResponseHeaders().replace(removeXCat, ""), removeXCat);
355376
let respond: GM_Types.XHRResponse = {
356377
finalUrl: config.url,
357378
readyState: <any>xhr.readyState,
358379
status: xhr.status,
359380
statusText: xhr.statusText,
360-
responseHeaders: xhr.getAllResponseHeaders(),
381+
responseHeaders: xhr.getAllResponseHeaders().replace(removeXCat, ""),
361382
responseType: config.responseType,
362383
};
363384
if (xhr.readyState === 4) {
364-
let contentType = xhr.getResponseHeader("Content-Type");
365-
if ((!config.responseType && contentType && contentType.indexOf("application/json") !== -1)) {
385+
if (config.responseType == "arraybuffer" || config.responseType == "blob") {
386+
if (xhr.response instanceof ArrayBuffer) {
387+
respond.response = URL.createObjectURL(new Blob([xhr.response]));
388+
} else {
389+
respond.response = URL.createObjectURL(xhr.response);
390+
}
391+
setTimeout(() => {
392+
URL.revokeObjectURL(respond.response);
393+
}, 60e3)
394+
} else if (config.responseType == "json") {
366395
try {
367-
respond.response = JSON.parse(xhr.responseText);
396+
respond.response = JSON.parse(xhr.response);
368397
} catch (e) {
369398
}
370399
} else {
371-
if (!respond.response && (config.responseType == "arraybuffer" || config.responseType == "blob")) {
372-
if (xhr.response instanceof ArrayBuffer) {
373-
respond.response = URL.createObjectURL(new Blob([xhr.response]));
374-
} else {
375-
respond.response = URL.createObjectURL(xhr.response);
376-
}
377-
setTimeout(() => {
378-
URL.revokeObjectURL(respond.response);
379-
}, 60e3)
380-
} else {
381-
respond.response = xhr.response;
382-
}
400+
respond.response = xhr.response;
383401
}
384402
try {
385403
respond.responseText = xhr.responseText;
386404
} catch (e) {
387-
388405
}
389406
}
390407
return respond;
@@ -432,7 +449,6 @@ export class BackgroundGrant {
432449

433450
let xhr = new XMLHttpRequest();
434451
xhr.open(config.method || 'GET', config.url, true, config.user || '', config.password || '');
435-
xhr.responseType = config.responseType || '';
436452
config.overrideMimeType && xhr.overrideMimeType(config.overrideMimeType);
437453

438454
let _this = this;
@@ -502,7 +518,7 @@ export class BackgroundGrant {
502518
xhr.setRequestHeader("X-Cat-" + this.rand + "-Cookie", config.cookie);
503519
}
504520
if (config.anonymous) {
505-
xhr.setRequestHeader("X-Cat-" + this.rand + "-Anonymous", "true")
521+
xhr.setRequestHeader("X-Cat-" + this.rand + "-Anonymous", "true");
506522
}
507523
if (config.overrideMimeType) {
508524
xhr.overrideMimeType(config.overrideMimeType);

0 commit comments

Comments
 (0)