Skip to content

Commit

Permalink
🐛 修复GM xhr header为空时发送默认值与UserConfig默认值的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Nov 8, 2022
1 parent 8b1a73c commit e3a04db
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
4 changes: 3 additions & 1 deletion example/gm_xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ GM_xmlhttpRequest({
data: data,
headers: {
"referer": "http://www.example.com/",
"origin": "www.example.com"
"origin": "www.example.com",
// 为空将不会发送此header
"sec-ch-ua-mobile": "",
},
onload(resp) {
console.log("onload", resp);
Expand Down
5 changes: 1 addition & 4 deletions src/pages/components/UserConfigPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ const UserConfigPanel: React.FC<{
const valueCtrl = IoC.instance(ValueController) as ValueController;
Object.keys(saveValues).forEach((key) => {
Object.keys(saveValues[key]).forEach((valueKey) => {
if (
saveValues[key][valueKey] === undefined ||
saveValues[key][valueKey] === values[`${key}.${valueKey}`]
) {
if (saveValues[key][valueKey] === undefined) {
return;
}
valueCtrl.setValue(
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/utils/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export function prepareScriptByCode(
let flag = true;
if (uuid !== undefined) {
old = await dao.findByUUID(uuid);
if (!old) {
if (old) {
flag = false;
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/runtime/background/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function listenerWebRequest(headerFlag: string) {
let anonymous = false;
let isGmXhr = false;
const requestHeaders: chrome.webRequest.HttpHeader[] = [];
const preRequestHeaders: { [key: string]: string } = {};
const preRequestHeaders: { [key: string]: string | null } = {};
details.requestHeaders?.forEach((val) => {
const lowerCase = val.name.toLowerCase();
if (lowerCase.startsWith(`${headerFlag}-`)) {
Expand All @@ -99,7 +99,7 @@ export function listenerWebRequest(headerFlag: string) {
isGmXhr = true;
break;
default:
preRequestHeaders[headerKey] = val.value || "";
preRequestHeaders[headerKey] = val.value || null;
break;
}
return;
Expand All @@ -116,8 +116,11 @@ export function listenerWebRequest(headerFlag: string) {
lowerCase.startsWith("sec-") ||
lowerCase.startsWith("proxy-")
) {
preRequestHeaders[lowerCase] =
preRequestHeaders[lowerCase] || val.value || "";
// null表示不发送此header
if (preRequestHeaders[lowerCase] !== null) {
preRequestHeaders[lowerCase] =
preRequestHeaders[lowerCase] || val.value || "";
}
} else {
requestHeaders.push(val);
}
Expand Down Expand Up @@ -149,10 +152,13 @@ export function listenerWebRequest(headerFlag: string) {
});
}
Object.keys(preRequestHeaders).forEach((key) => {
requestHeaders.push({
name: key,
value: preRequestHeaders[key],
});
// null表示不发送此header
if (preRequestHeaders[key] !== null) {
requestHeaders.push({
name: key,
value: preRequestHeaders[key]!,
});
}
});
return {
requestHeaders,
Expand Down

0 comments on commit e3a04db

Please sign in to comment.