chrome扩展onInstalled事件 [英] chrome extension onInstalled event

查看:336
本文介绍了chrome扩展onInstalled事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对chrome扩展程序安装/更新事件有疑问.如果我在后台脚本的顶级代码中添加onInstalled事件侦听器,那么是否存在一个时间范围内我的事件侦听器将捕获该事件?

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?

我问这个问题是因为我的演示表明,如果在挂接InstallInstalled侦听器之前有一些逻辑可以执行,则看起来该逻辑将永远不会执行,就像该事件同时发生一样.

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.

谢谢!

更新@Noam Hacker:由于公司政策,我无法在此处发布任何真实代码,但是我有一些伪代码可以说明我的问题:

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侦听器在以下情况下捕获事件:

onInstalled listeners catch events in these situations:

首次安装扩展程序时,将扩展程序更新到新版本以及将Chrome更新到新版本时.

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.

链接到文档

根据您的问题,似乎您需要帮助以正确的顺序执行代码. 此答案为您的案例提供了一个有用的框架(使用reason属性).

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
    }
});

这篇关于chrome扩展onInstalled事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆