Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

javascript - Adding context menu item on a non-persistent background script?

I am adding a context menu item from a non-persistent background script using:

chrome.contextMenus.create({
  title: 'Get Code',
  id: 'myUniqueIdForThisExtension123',
  contexts: ['all'],
  onclick: onClickHandler
});

function onClickHandler() {}

The documentation simply states:

The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension.

So I added a unique ID, but I still cannot make it work. Nothing new inserted on the context menu.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are saying you've got a non-persistent background page, so you should have a manifest.json file that looks like this:

{
    "manifest_version": 2,

    "name": "My extension",
    "description": "My description",
    "version": "1",
    
    "permissions": ["contextMenus"],
    "background": {
        "persistent": false,
        "scripts": [
            "/background.js"
        ] 
    }
}

Now, since you've got a non-persistent background page, you have to use the right listeners to "wake it up" when you need to use the context menu.

Quoting from the official documentation (NOTE: this link points to an archived copy of the documentation from 2014 and is outdated):

Best practices when using event pages:

[...]

  1. If you're using the context menus API, pass a string id parameter to contextMenus.create, and use the contextMenus.onClicked callback instead of an onclick parameter to contextMenus.create.

So, simply, your error is using the onclick parameter instead of the contextMenus.onClicked.addListener() method in your background.js.

Solution

I already said that you should use the onClicked event, but I would like to add that, referring to x a's answer:

You should register a context menu via contextMenus.create in the event handler of runtime.onInstalled, as these context menu registrations persist anyway.

So simply, given that once created the context menus persist in your extension, it's best practice to only define them a single time: when your extension is installed (or updated), and add the listener each time the background page is loaded.

chrome.runtime.onInstalled.addListener(function() {
    chrome.contextMenus.create({
        title: 'My menu',
        id: 'menu1', // you'll use this in the handler function to identify this context menu item
        contexts: ['all'],
    });
});

chrome.contextMenus.onClicked.addListener(function(info, tab) {
    if (info.menuItemId === "menu1") { // here's where you'll need the ID
        // do something
    }
});

That's what you need to create a context menu in an event page, as shown in the documentation page of the chrome.contextMenus API.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...