【问题标题】:chrome extension onInstalled eventchrome 扩展 onInstalled 事件
【发布时间】:2019-06-01 16:15:55
【问题描述】:

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

我问这个,因为我的演示表明,如果我在挂钩 onInstalled 侦听器之前执行了一些逻辑,它看起来永远不会被执行,就像同时发生的事件一样。

有人可以在后台脚本中的其他逻辑的上下文中更详细地向我解释这个事件是如何工作的,或者向我指出一些文档,因为我找不到任何有用的东西。

谢谢!

更新@Noam Hacker:由于公司政策,我不能在这里发布任何真实代码,但我有一些伪代码可以说明我的问题:

/**
 * 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
}

【问题讨论】:

  • 你有后台脚本的示例代码吗?
  • 如果你把你的主要逻辑放在监听器函数里面呢? chrome.runtime.onInstalled.addListener(function (details) { //perform logic you'd like to do first... //install/update logic... }
  • @NoamHacker 如果我将一些我想首先执行的逻辑放在监听器函数中,我的测试表明,如果在监听器中还有其他一些逻辑,则不能保证这个逻辑会首先执行背景。
  • 我看到你提到了异步生成器/承诺。您是否还执行chrome.runtime.onInstalled.addListener(...) async,意思是在生成器/承诺或回调中?根据我的经验,当“顶级”后台脚本执行时,需要同步注册监听器。 onInstalled 回调执行当然是异步的,因为事件仅在初始同步脚本执行后触发。
  • @JoelPurra 你是对的,监听器需要在后台脚本的顶级代码中同步注册。自从我遇到这个问题以来已经有一段时间了,但问题是我在顶级代码之外添加了一些侦听器,这些侦听器是通过通过 Promise 编排的函数,这导致 onInstalled 事件在处理此事件的逻辑被挂钩之前执行。

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

onInstalled 监听器在这些情况下捕获事件:

第一次安装扩展程序时、扩展程序更新到新版本时以及 Chrome 更新到新版本时。

由于这都是异步的,它将在后台发生,并且根据文档,在任何这些情况下都会立即触发。回顾一下异步编程,了解一下这一点。

link to documentation

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

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

【讨论】:

  • 我知道这一点,onInstalled 事件会在扩展安装、更新或 Chrome 更新到新版本时触发。我对流程以及可以捕获该事件的时间范围更加好奇,因为我注意到如果我在挂钩 onInstalled 侦听器之前运行了一些逻辑,我的侦听器永远不会捕获该事件。
  • 我似乎遇到了类似的问题。当我执行“加载解包”时,它总是会触发,但是一旦我打包代码并进行一些关闭,onInstalled 会触发大约一半的时间,而控制台中不会出现任何错误。在启动侦听器之前我还有一些代码正在运行,所以我正在移动我的代码以确保侦听器在进程的早期设置。
【解决方案2】:

我也需要弄清楚这一点。虽然我没有找到任何权威,但我确实在我的后台脚本中抛出了几个 console.time() 语句。

代码是这样的:

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

然后我只是加载/重新加载了几次扩展(解压,在开发模式下)。 onInstall 似乎在前 50 毫秒内非常可靠地触发,而第一个函数发生在第一个毫秒内。结果如下:

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

【讨论】:

    【解决方案3】:

    鉴于the document

    “监听器必须从页面开始同步注册。”

    “不要异步注册监听器,因为它们不会被正确触发。”

    ,它似乎保证每个同步连接的侦听器都不会错过任何一个,无论评估您的代码需要多长时间。这将通过 Chrome 触发事件评估您的整个代码来完成。

    我的假设是 onInstalled 实际上像 onInitialized 一样工作。不过没有测试数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-27
      • 2013-11-30
      • 2017-03-20
      • 2012-01-12
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      相关资源
      最近更新 更多