如何在onStartup和onInstalled上添加Chrome扩展侦听器? [英] How can I add a Chrome extension listener both onStartup and onInstalled?
问题描述
chrome.runtime API 有一些事件可用于将侦听器添加到上下文菜单。目前我正在使用 chrome.runtime.onStartup
如下:
chrome.runtime.onStartup.addListener(function(){
chrome.contextMenus.create({$ b $'title':'Add:%s',
'contexts':['选择']
});
});
chrome.contextMenus.onClicked.addListener(onClickHandler);
问题在于 chrome.runtime.onStartup
将在用户启动或重新启动Chrome时起作用,并且在首次安装或更新扩展程序或Chrome时, chrome.runtime.onInstalled
将可用。
如果我只做 onStartup
,那么当我的扩展程序或Chrome下一次更新时,我的上下文菜单将不存在。如果我只做 onInstalled
,那么在用户重新启动Chrome之后,我的上下文菜单将不会持续。
如何才能我处理这两种情况?
这实际上是一个有趣的问题,因为行为会根据您是否使用< href =https://developer.chrome.com/extensions/event_pages =noreferrer>活动页面或持久背景页面。
请注意,这个答案是特定于 contextMenu
API!
持久背景页面
然后它会在您的扩展的后台页面加载时执行。
如果您有一个持久的后台页面,这正是你想要的:无论什么原因,当扩展开始时执行一次 id 属性在 create()
中。
Google有一个相应示例。
活动页面
在扩展的整个生命周期中,事件页面的加载次数比常规页面的次数要多得多。无论如何,上下文菜单API需要对事件页面进行特殊处理。
首先,包括 id $>对于事件页面, contextMenus.create()
中的c $ c>属性是要求。另外,因为代码在空闲时被卸载,所以您必须使用 chrome.contextMenus.onClicked
而不是 onclick
属性。
文档建议使用 onInstalled
:
如果您在安装或升级扩展程序时需要进行一些初始化,请收听 runtime.onInstalled
事件。这是注册 declarativeWebRequest
规则, contextMenu
条目以及其他类似内容的好地方一次性初始化。
确实,这就是他们在示例。我测试了它,通过重新启动扩展和浏览器,菜单仍然存在。不过,这种区别并没有明确记录。
Bug警报!鉴于Rob W对此错误,如果扩展功能被禁用,则该方法不是100%可靠。
The chrome.runtime API has a few events that I can use to add a listener to a context menu. At the moment I'm using chrome.runtime.onStartup
like so:
chrome.runtime.onStartup.addListener(function() {
chrome.contextMenus.create({
'title': 'Add: %s',
'contexts': ['selection']
});
});
chrome.contextMenus.onClicked.addListener(onClickHandler);
The problem is that chrome.runtime.onStartup
will work when the user starts or restarts Chrome, and chrome.runtime.onInstalled
will work when the extension or Chrome is first installed or updated.
If I only do onStartup
, then my context menu won't be there when my extension or Chrome is next updated. If I only do onInstalled
, then my context menu won't persist after the user restarts Chrome.
How can I handle both cases?
解决方案 This is actually an interesting question, since the behavior differs depending on whether you're using an event page or a persistent background page.
Note that this answer is specific to contextMenu
API!
Persistent background page
You should simply put your code on the top level.
Then it will execute every time your extension's background page is loaded.
If you have a persistent background page, that's exactly what you want: execute once when the extension starts for whatever reason.
If you want to make sure you don't create a duplicate item, include an id
attribute in create()
.
Google has a corresponding sample.
Event page
Event page is loaded much more often than a regular one throughout the lifetime of an extension. And anyway, context menu API requires special treatment with event pages.
First off, including an id
attribute in contextMenus.create()
is a requirement for event pages. Also, because code is unloaded when idle, you have to use chrome.contextMenus.onClicked
instead of an onclick
attribute.
Documentation recommends using onInstalled
:
If you need to do some initialization when your extension is installed or upgraded, listen to the runtime.onInstalled
event. This is a good place to register for declarativeWebRequest
rules, contextMenu
entries, and other such one-time initialization.
Indeed, that's what they do in the sample.
I tested it, and indeed the context menus persist through restart of the extension and the browser. This difference is not explicitly documented, though.
Bug Alert! In view of Rob W's comment about this bug, the method is not 100% reliable if the extension happens to be disabled.
这篇关于如何在onStartup和onInstalled上添加Chrome扩展侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!