javascript - Using declarativeNetRequest to "modifyHeaders" dynamically by a function call - Stack Overflow

I'm mocking a Chrome extension which should modify HTTP request headers dynamically. The extension

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.

Share Improve this question asked Mar 14 at 11:17 StanStan 8,76810 gold badges62 silver badges105 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信