最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I add a Chrome extension listener both onStartup and onInstalled? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked May 3, 2015 at 12:55 BenjaminBenjamin 3,9502 gold badges27 silver badges46 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 10

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 remends 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 ment about this bug, the method is not 100% reliable if the extension happens to be disabled.

发布评论

评论列表(0)

  1. 暂无评论