Stephen Collins
Stephen Collins

Reputation: 3653

Chrome Extension context menu does not persist

In my background.js file I have the following code:

chrome.runtime.onInstalled.addListener(function (info) {
    chrome.contextMenus.removeAll(function () {
        chrome.contextMenus.create({
            "title": "Blog this image",
            "contexts": ["image"],
            "id": "contextimagelink",
            "onclick": onImageClickedHandler
        }, function () { });
    });
});

The idea is that any time the extension is installed or updated to remove any existing context menus I've set up before and install only the one.

The problem is that every time the extension is updated through the store, the context menus are removed, but not reinstalled. I can't figure out a way to debug this, or why it is happening at all. It does not happen ever during development, only when the extension is updated after I've published an update.

Upvotes: 2

Views: 507

Answers (1)

Rob W
Rob W

Reputation: 348972

Add the context menu at the chrome.runtime.onInstalled and chrome.runtime.onStartup events.

For sample code, take a look at https://github.com/Rob--W/crxviewer/blob/master/src/bg-contextmenu.js In this extension, context menus are an optional feature, whose state is controlled through a preference persisted via the chrome.storage API.
Also, there's an open Chromium issue with the onInstalled event in incognito mode. I've solved this problem in https://github.com/Rob--W/crxviewer/blob/master/src/incognito-events.js

Upvotes: 2

Related Questions