Update chrome.webRequest header modification from V2 to V3

1.3k views Asked by At

I'm trying to update my chrome extension from V2 to V3.

The part that doesn't seem to work right now is where I modify incoming headers

V2:

function modifyHeadersForUrls(urls: string[]) {  
    chrome.webRequest.onHeadersReceived.addListener((details) => {
        details.responseHeaders.testHeader = 'barfoo';
        return details.responseHeaders;
    }, { urls }, ['blocking', 'responseHeaders']);
}

For this code to work in V2 I needed to add 2 permission to my manifest:

"webRequest",
"webRequestBlocking"

In V3 I get the impression that I should switch to

 "declarativeNetRequest",
 "declarativeNetRequestWithHostAccess",
 "declarativeNetRequestFeedback"

Or at least to one of them. I think I need declarativeNetRequestWithHostAccess but I'm not sure what I should do here. It is needed to declare a declarative_net_request block in the manifest, but in my case the urls are dynamic.

Normally I can figure it out with the docs and a couple of examples, but the problem is, is that I can't find any example using declarativeNetRequestWithHostAccess.

Any help would be appreciated!

Based on the example given below I was able to produce the following code

const urls = ['localhost:8000'];

chrome.declarativeNetRequest.updateDynamicRules({
  // chrome.declarativeNetRequest.updateSessionRules({
  // removeRuleIds: ....,
  addRules: [
    {
      id: 1,
      priority: 1,
      condition: {
        initiatorDomains: urls,
        resourceTypes: ['main_frame']
      },
      action: {
        type: 'modifyHeaders',
        responseHeaders: [
          { header: 'Content-Security-Policy', operation: 'remove' },
          { header: 'Content-Security-Policy-Report-Only', operation: 'remove' },
        ],
      }
    }
  ]
});

In my case I need to remove the CSP headers for a given url. What the above code is suppose to do, is to remove these headers for any url from localhost:8000? Unfortunate, it didn't work. Any ideas what might be wrong in my code?

Also, it is unclear what exactly updateSessionRules does? What is a session in terms of the background service worker?

Thanks alot!

0

There are 0 answers