[英]How can I add a Chrome extension listener both onStartup and onInstalled?
The chrome.runtime API has a few events that I can use to add a listener to a context menu. chrome.runtime API有一些事件可用于将监听器添加到上下文菜单。 At the moment I'm using
chrome.runtime.onStartup
like so: 目前我正在使用
chrome.runtime.onStartup
如下所示:
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. 问题是
chrome.runtime.onStartup
将在用户启动或重新启动Chrome时运行,并且chrome.runtime.onInstalled
将在首次安装或更新扩展程序或Chrome时chrome.runtime.onInstalled
。
If I only do onStartup
, then my context menu won't be there when my extension or Chrome is next updated. 如果我只进行
onStartup
,那么当我的扩展程序或Chrome下次更新时,我的上下文菜单就不会出现。 If I only do onInstalled
, then my context menu won't persist after the user restarts Chrome. 如果我只进行
onInstalled
,那么在用户重启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! 请注意,此答案特定于
contextMenu
API!
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()
. 如果要确保不创建重复项,请在
create()
包含id
属性。
Google has a corresponding sample . 谷歌有相应的样本 。
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.
无论如何,上下文菜单API需要对事件页面进行特殊处理 。
First off, including an id
attribute in contextMenus.create()
is a requirement for event pages. 首先,在
contextMenus.create()
包含id
属性是事件页面的要求 。 Also, because code is unloaded when idle, you have to use chrome.contextMenus.onClicked
instead of an onclick
attribute. 此外,由于代码在空闲时卸载,您必须使用
chrome.contextMenus.onClicked
而不是onclick
属性。
Documentation recommends using onInstalled
: 文档建议使用
onInstalled
:
If you need to do some initialization when your extension is installed or upgraded, listen to the
runtime.onInstalled
event.如果在安装或升级扩展时需要进行一些初始化,请侦听
runtime.onInstalled
事件。 This is a good place to register fordeclarativeWebRequest
rules,contextMenu
entries , and other such one-time initialization.这是注册
declarativeWebRequest
规则,contextMenu
条目以及其他此类一次性初始化的好地方。
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.
鉴于Rob W对此错误的评论,如果扩展恰好被禁用,则该方法不是100%可靠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.