declarativeNetRequest - dynamic rule not resulting in a redirect

I’m updating a browser extension from Manifest v2 to v3 and have therefore implemented dynamic rules as follows:

const extensionUrl = chrome.runtime.getURL('/content/redirect.html');

chrome.declarativeNetRequest.updateDynamicRules({
    removeRuleIds: [ 1 ],
    addRules: [
        {
            action: {
                type: 'redirect',
                redirect: {
                    regexSubstitution: `${extensionUrl}?\\1`
                }
            },
            condition: {
                regexFilter: '^https:\\/\\/mail\\.google\\.com\\/mail\\/\\?(affixa=.*)',
                resourceTypes: [ 'main_frame' ]
            },
            id: 1,
            priority: 1
        }  
    ]
});

In Chrome (131) when executed as a service worker, a URL such as the following is handled by the above rule - i.e. it is redirected to a page within the extension and includes the querystring in the URL.

https://mail.google.com/mail/?affixa=-1225818694#drafts/?compose=GTvVlcSMSqLWmXnCXpcwWVXqTKRWNQfQMTXdlQtCtTthzDfGrcgcdrdfqCLXqwtmhTMwGlQGWDKtB

In Firefox (132) when executed as a background page, nothing happens at all. No redirect happens and neither does the browser navigate to the original URL. All that does happen is, if I open a new tab, enter the above URL and press enter, any home screen content such as search box, news stories, etc, disappears and I get an empty screen. There’s no errors in the console for that tab, no errors when inspecting the background page, etc.

If I manually enter the address that the browser should have redirected to:

moz-extension://1c15cdd3-8b1e-435b-8e91-c9fa44dc64b2/content/redirect.html?affixa=-1225818694#drafts/?compose=GTvVlcSMSqLWmXnCXpcwWVXqTKRWNQfQMTXdlQtCtTthzDfGrcgcdrdfqCLXqwtmhTMwGlQGWDKtB

the page works absolutely fine and completes the use case its designed to do.

Can anybody suggest what might be going wrong here?

The pertinent bits of my manifest file are as follows:

"host_permissions": [
  "*://mail.google.com/*", 
  "*://accounts.google.com/*" 
],
"permissions": [ 
    "alarms", 
    "declarativeNetRequest",
    "storage",
    "tabs", 
    "webRequest"
],

The extension has been loaded unpacked using about:debugging.

Thanks in advance!

Chris

Could you share your manifest.json file, at least the web_accessible_resources part of it?

1 Like

Hi Rob,

Thanks for replying! I actually didn’t have anything in the manifest for web_accessible_resources at all. Adding it in has made things work, so job done, though it’s curious that it wasn’t necessary for Chrome and that no visible error was raised in Firefox.

For reference, this is what I added:

 "web_accessible_resources": [
    {
        "resources": [ "content/redirect.html" ],
        "matches" : [ "*://mail.google.com/*" ]
    }
 ]

Nevertheless, happy to have progressed! Thanks for your help.

Chris