Google chrome extension race condition

Viewed 138

I have a google chrome extension. In the background.js file, it contains this listener:

chrome.webRequest.onBeforeRequest.addListener(function(info)
    {
        console.log("On Before request info", info);
    },
    {
        urls: 
        [ 
            "<all_urls>" 
        ]
    },
    [
        "blocking"
    ]
);

I tell google chrome to open using the "--app" command line parameter and the html page it opens contains this:

<script type="text/javascript">
    let url = new URL(window.location.href);

    let targetUrl = url.searchParams.get("targetUrl");

    if(targetUrl != null)
    {
        targetUrl = unescape(targetUrl);

        console.log("The target url is:", targetUrl);
        
        window.location.href = targetUrl;
    }
</script>

It gets a url from the from the params and then navigates to it.

The issue is that my google chrome extension's listener is not being fired.

I found that if I set it to do the redirect with a 1 second timeout.

Then it fires as its suppose to.

This makes me believe that the background.js file of my extension is not executed before the first page is loaded.

How can I make google chrome assure that all the extensions are loaded and initialized before the first page is ever loaded?

0 Answers
Related