2

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!

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
  • 1
    Here's an [example](https://stackoverflow.com/a/69177790). – wOxxOm May 08 '22 at 12:10
  • Thanks a lot for the example. Based on your code I was able to rewrite my code, but I couldn't get it to work. I've updated my question with my findings. Maybe you could have an other look? Thnx – Jeanluca Scaljeri May 08 '22 at 20:51
  • 1
    Domain can't include a port, AFAIK, so you should remove `:8000`. – wOxxOm May 09 '22 at 00:03

0 Answers0