Is there any way to always get webRequest.onBeforeRequest event when Mozilla Firefox is started by link?

Hello. In the process of developing, I faced a next problem.

I’ve created an add-on: 1) manifest.json:
{
“manifest_version”: 2,
“name”: “Example”,
“version”: “1.0”,
“description”: “Example”,
“permissions”: [
“tabs”,
“webRequest”,
“webRequestBlocking”,
“<all_urls>”
],
“icons”: {
“48”: “icon.png”
},
“background”: {
“scripts”: [
“background.js”
]
},
“applications”: {
“gecko”: {
“id”: "my-example@ex.com",
“strict_min_version”: “52.0”
}
}
}

  1. background.js:

    console.log("The first message ");

    browser.webRequest.onBeforeRequest.addListener(
    cancelReq,
    { urls: ["<all_urls>"], types: [“main_frame”] },
    [“blocking”]
    );

    function cancelReq(details) {
    console.log("Details: " + JSON.stringify(details));
    return { cancel: true };
    }

Now, I click on the link, e.g ‘http://www.google.com/’ when Firefox is closed. 1 out of 10 times on average Firefox is opened and the first URL is not fired.
I see the console:

16:02:44.750 Something… (It depends on the link page.)

16:02:45.491 The first message

Is there any way to always get this event?

Getting your background scripts as persistent should fix this issue :

Can you elaborate a bit more on how to use “persistent” and what it does? I’m assuming something like “persistent”:true, but the page doesn’t really give much detail and any insight would be appreciated.

Sure thing! You are right to say the documentation is somewhat lacking.

As you said, by using persistent”:true in your manifest, then using a background page instead of just scripts, it should do the trick.

Note that using persistent is maybe not the perfect solution here, as there are some talks to deprecation Persistent Background page with Manifest V3 (at least for Google Chrome).

1 Like

sylvaing, thank you very much. It really works.

Thanks for elaborating!

I’m glad I could help!