I'm mocking a Chrome extension which should modify HTTP request headers dynamically. The extension works as expected with a static value provided in "modifyHeaders" rule, but I can't figure out how to calculate the value dynamically via function. All attempts leads to errors "Uncaught SyntaxError: Unexpected identifier 'getValue'" (if I define the function) or 'self' (if I embed the expression directly into the rule).
const MY_CUSTOM_RULE_ID = 1
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [MY_CUSTOM_RULE_ID],
addRules: [
{
id: MY_CUSTOM_RULE_ID,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [
{
getValue: async function() { return btoa(String.fromCharCode(...new Uint8Array((await self.crypto.subtle.digest("SHA-256", new TextEncoder().encode("my custom header value")))))); },
operation: "set",
header: "my custom name",
value: await getValue()
}
]
},
condition: {
"resourceTypes": ["main_frame", "sub_frame"]
},
}
],
});
The above code is the current background.js
. The final task implies that the value is calculated via crypto.
I'm mocking a Chrome extension which should modify HTTP request headers dynamically. The extension works as expected with a static value provided in "modifyHeaders" rule, but I can't figure out how to calculate the value dynamically via function. All attempts leads to errors "Uncaught SyntaxError: Unexpected identifier 'getValue'" (if I define the function) or 'self' (if I embed the expression directly into the rule).
const MY_CUSTOM_RULE_ID = 1
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [MY_CUSTOM_RULE_ID],
addRules: [
{
id: MY_CUSTOM_RULE_ID,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [
{
getValue: async function() { return btoa(String.fromCharCode(...new Uint8Array((await self.crypto.subtle.digest("SHA-256", new TextEncoder().encode("my custom header value")))))); },
operation: "set",
header: "my custom name",
value: await getValue()
}
]
},
condition: {
"resourceTypes": ["main_frame", "sub_frame"]
},
}
],
});
The above code is the current background.js
. The final task implies that the value is calculated via crypto.
1 Answer
Reset to default 0I think you cannot use a function inside chrome.declarativeNetRequest
rules. The API requires static values, meaning all headers must be predefined.
Since declarativeNetRequest
doesn’t support dynamic values, switch to chrome.webRequest.onBeforeSendHeaders
, which allows modifying headers with a function:
chrome.webRequest.onBeforeSendHeaders.addListener(
async function(details) {
const encoder = new TextEncoder();
const hashBuffer = await crypto.subtle.digest("SHA-256", encoder.encode("my custom header value"));
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashBase64 = btoa(String.fromCharCode(...hashArray));
details.requestHeaders.push({ name: "my custom name", value: hashBase64 });
return { requestHeaders: details.requestHeaders };
},
{ urls: ["<all_urls>"] },
["blocking", "requestHeaders"]
);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744660242a4586415.html
评论列表(0条)