简体   繁体   中英

chrome extension onInstalled event

I have a question about chrome extension install/update event. If I add the onInstalled event listener in a top level code in the background script, is there a time frame in which my event listener will catch that event?

I'm asking this, because my demos showed that if I have some logic that executes before I hook onInstalled listener, it looks like it will never be executed, like that event happens in the meantime.

Can someone explain to me with more details how this event works, in the context of other logic in the background script, or point me to some documentation, since I haven't been able to find anything useful.

Thanks!

Update @Noam Hacker : Due to a company policy I can't post any real code here, but I have some pseudo code that illustrates my problem :

/**
 * setup in which I miss onInstalled event
 */
function firstLogicThatRunsOnBackgroundLoad() {
    // perform some logic

    // perform some asynchronous operations via generators and promises
    // which can take a while

    chrome.runtime.onInstalled.addListener(function (details) {
            if (details.reason == "install") {
                // this logic never gets executed
            } else if(details.reason == "update") {
                // perform some logic
            }
        });
}

/**
 * setup in which I catch onInstalled event 
 */
function firstLogicThatRunsOnBackgroundLoad() {
    chrome.runtime.onInstalled.addListener(function (details) {
            if (details.reason == "install") {
                // this logic executes
            } else if(details.reason == "update") {
                // perform some logic
            }
        });

    // perform some logic

    // perform some asynchronous operations via generators and promises
    // which can take a while
}

onInstalled listeners catch events in these situations:

when the extension is first installed, when the extension is updated to a new version, and when Chrome is updated to a new version.

Since this is all asynchronous it will happen in the background, and according the documentation, fires immediately at any of these situations. Review asynchronous programming for some clarity on this.

link to documentation

According to your question it seems like you want help executing code in the right order. This answer provides a helpful framework for your case (using the reason attribute).

chrome.runtime.onInstalled.addListener(function(details){
    if(details.reason == "install"){
        //call a function to handle a first install
    }else if(details.reason == "update"){
        //call a function to handle an update
    }
});

I needed to figure this out too. While I didn't find anything authoritative, I did throw a couple of console.time() statements in my background script.

Code was something like this:

console.time('onInstall event');
console.time('first function');

chrome.runtime.onInstalled.addListener(details => {
  console.timeEnd('onInstall event');
});

// 7 module imports

someSyncFunction() // console.timeEnd('first function') is called in the first line in this function

Then I just loaded/reloaded the extension (unpacked, in dev mode) a few times. onInstall seems to pretty reliably fire within the first 50ms, while the first function happens w/in the first ms. Here are the results:

(First function, onInstall event)
(.282ms, 47.2ms)
(.331ms, 45.3ms)
(.327ms, 49.1ms)
(.294ms, 45.9ms)

Given that the document says

“Listeners must be registered synchronously from the start of the page.”

and

“Do not register listeners asynchronously, as they will not be properly triggered.”

, it seems they guarantee every synchronously-attached listener not to miss any, no matter how long it takes to evaluate your code. And this would be done by Chrome firing events after evaluating your entire code.

My hypothesis is that onInstalled actually works like onInitialized . No test data, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM