Read settings from browser storage in onBeforeRequest callback

  firefox-addon, javascript, webapi

I’m trying to read a setting from the browser storage before redirecting to a different page in my Firefox add-on.

(async function() {
    function listener(details) {
        //let foo = await browser.storage.sync.get({ domain: "https://stackexchange.com/" });
        //let foo = await Promise.resolve("https://stackexchange.com/")
        let foo = "https://stackexchange.com/"

        return { redirectUrl: foo }

    }

    browser.webRequest.onBeforeRequest.addListener(
        listener, {
            urls: [
                "*://example.com/*",
            ],
            types: ["main_frame"]
        },

        ["blocking"]
    );
})()

The Immediately invoked function expression is to enter a context in which I can use async. If I use one of the await lines, there is not redirect at all. The third line does work.
The script is loaded as a background script.

Source: Ask Javascript Questions

LEAVE A COMMENT