declarativeNetRequest.updateSessionRules do not persist on new tabs

64 views
Skip to first unread message

Uğur

unread,
Jun 19, 2024, 2:19:55 AM (10 days ago) Jun 19
to Chromium Extensions
Hey!

I modify an header in my background script when my popup sends a message. (on a user clicks a button in my popup)

```
  chrome.declarativeNetRequest.updateSessionRules(
          {
            addRules: [
              {
                id: 1,
                priority: 1,
                action: {
                  type: "modifyHeaders",
                  requestHeaders: [
                    {
                      header: "placeholder",
                      operation: "set",
                      value: "placeholder",
                    },
                  ],
                },
                condition: {
                  urlFilter: "*",
                  resourceTypes: [
                    "main_frame",
                    "sub_frame",
                    "stylesheet",
                    "script",
                    "image",
                    "font",
                    "object",
                    "xmlhttprequest",
                    "ping",
                    "csp_report",
                    "media",
                    "websocket",
                    "other",
                  ],
                },
              },
            ],
            removeRuleIds: [1],
          },
          () => {
           
            chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
              if (tabs[0]) {
                chrome.scripting.executeScript({
                  target: { tabId: tabs[0].id },
                  func: () => location.reload(),
                })
              }
            })

            sendResponse({ success: true })
          }
        )
```

which after I modify the header, I reload the page and the modified header will work without any issue. 


BUT if I go to a existed tab and make some search etc. / go to a url, my header is not here. Even though I can see my rule is there by doing this:

```
 chrome.declarativeNetRequest.getSessionRules((rules) => {
    console.log("rules", rules)
  })
```

why new tabs OR existed tabs don't get affected by modifying headers? 

I also tried changing ```condition``` part to be using "tabIds" and I appended new tabs into my rule after removing old rule. but it also doesn't work.

What can we do about this?

Thanks!

woxxom

unread,
Jun 19, 2024, 2:36:27 AM (10 days ago) Jun 19
to Chromium Extensions, Uğur
The site is probably using a service worker, in which case you'll have to unregister it explicitly by calling chrome.browsingData.removeServiceWorkers.
Reply all
Reply to author
Forward
0 new messages