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:
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!
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 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 fordeclarativeWebRequest
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.