chrome.runtime.onStartup never fires?

蓝咒 提交于 2019-12-21 11:00:13

问题


Consider the following Chrome extension:

manifest.json

{
    "name": "Test onStartup",
    "version": "0.0.0",
    "manifest_version": 2,
    "background": {
        "persistent": false,
        "scripts": ["eventPage.js"]
    },
    "permissions": ["storage"]
}

eventPage.js

chrome.runtime.onStartup.addListener(function() { 
    console.log("I started up!");
    chrome.storage.local.set({"startedUp": true});
});

chrome.runtime.onStartup is documented as firing "when a profile that has this extension installed first starts up", and I would've suspected that it also fires upon reloading the extension. However, upon restarting the browser or reloading the extension, I do not see the console.log message in the _generated_background_page.html's console, and chrome.storage.local.get("startedUp", function(v) { console.log(v) }) yields no results, so I suspect that the listener was not called.

Am I misunderstanding when this event is triggered or binding to it incorrectly or anything like that? Is it an issue with Chrome 28.0.1500.71 on Linux?


回答1:


When you close chrome its background process keep on running. Make sure to kill all the process named as chrome or similar before starting chrome again.




回答2:


chrome.runtime.onStartup is only called when Chrome starts, not when the extension starts.

chrome.runtime.onInstalled is called when you manually reload the extension within chrome://extensions, or when the extension calls chrome.runtime.reload().




回答3:


Heads-up about this event which - in hindsight - is kinda obvious: your eventListener won't be fired if you register it after any async callbacks. You have to register it during the initial loading, outside of any callbacks.

In my case it wasn't getting fired because I was registering it as part of my "main" method, which was called after a settings validation, which requires a callback.



来源:https://stackoverflow.com/questions/17621545/chrome-runtime-onstartup-never-fires

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!