简体   繁体   English

如何在onStartup和onInstalled上添加Chrome扩展程序监听器?

[英]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!

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() . 如果要确保不创建重复项,请在create()包含id属性。

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. 无论如何,上下文菜单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 for declarativeWebRequest 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.

相关问题 如何为 Chrome 扩展测试 chrome.runtime.onInstalled? - How can I test chrome.runtime.onInstalled for a Chrome extension? chrome 扩展 onInstalled 事件 - chrome extension onInstalled event 如何在Chrome扩展程序的内容脚本中将事件监听器添加到Google表格链接? - How can I add event Listener to google sheets link in chrome extension's content script? 如何在Chrome扩展程序中更新onBeforeRequest事件侦听器? - How can I update an onBeforeRequest event listener in a chrome extension? 如何在Chrome扩展程序中使用Require.JS时触发chrome.runtime.onInstalled - How to get chrome.runtime.onInstalled to fire when using Require.JS in Chrome extension 在 chrome 扩展的后台脚本中,如何处理对 chrome.runtime.onInstalled 事件的 addListener() 方法的调用? - In the background script for a chrome extension, how does a call to the addListener() method for the chrome.runtime.onInstalled event get processed? Chrome扩展程序,添加侦听器无响应 - Chrome Extension, Add Listener Not Responding 无法在Chrome扩展程序中添加监听器 - cant' add listener in chrome extension 首次安装 chrome 扩展程序时如何添加监听器? - How to add listener for when chrome extension is first installed? 如何在Chrome扩展程序的弹出窗口中添加复选框的事件监听器? - How to add event listener for checkbox in Chrome extension's popup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM