admin管理员组

文章数量:1425900

I'm trying to add a context menu item to a chrome app and it's not showing up at all. Everything I've read seems to indicate that I'm doing the right thing here, but evidently I'm not.

background.js:

var clickHandler = function(e) {
  console.log('testing testing');
}
chrome.contextMenus.create({
  "title": "Click Me",
  "contexts": ["page", "selection", "image", "link"],
  "onclick" : clickHandler
});

manifest.json:

{
    "update_url": "",
    "name": "Test",
    "description": "Test",
    "manifest_version": 2,
    "version": "3.2.3",
    "kiosk_enabled": true,
    "icons": {
        "128": "icon_128.png",
        "16": "icon_16.png"
    },
    "app": {
        "background": {
            "scripts": [ "background.js" ],
            "persistent": false
        }
    },
    "permissions": [
        "http://*/", "https://*/", "webview", "storage", "power", "alwaysOnTopWindows", "idle", "contextMenus"
    ]
}

There are no errors being logged or anything like that. The item simply does not show up. What am I missing here?

I'm trying to add a context menu item to a chrome app and it's not showing up at all. Everything I've read seems to indicate that I'm doing the right thing here, but evidently I'm not.

background.js:

var clickHandler = function(e) {
  console.log('testing testing');
}
chrome.contextMenus.create({
  "title": "Click Me",
  "contexts": ["page", "selection", "image", "link"],
  "onclick" : clickHandler
});

manifest.json:

{
    "update_url": "https://clients2.google./service/update2/crx",
    "name": "Test",
    "description": "Test",
    "manifest_version": 2,
    "version": "3.2.3",
    "kiosk_enabled": true,
    "icons": {
        "128": "icon_128.png",
        "16": "icon_16.png"
    },
    "app": {
        "background": {
            "scripts": [ "background.js" ],
            "persistent": false
        }
    },
    "permissions": [
        "http://*/", "https://*/", "webview", "storage", "power", "alwaysOnTopWindows", "idle", "contextMenus"
    ]
}

There are no errors being logged or anything like that. The item simply does not show up. What am I missing here?

Share Improve this question edited Aug 21, 2023 at 8:32 woxxom 74.1k14 gold badges156 silver badges160 bronze badges asked Mar 10, 2015 at 1:42 cloudwalkercloudwalker 2,4765 gold badges37 silver badges76 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You missed a small note in the contextMenu documentation:

function (optional) onclick
A function that will be called back when the menu item is clicked. Event pages cannot use this; instead, they should register a listener for chrome.contextMenus.onClicked

You do have an Event page ("persistent": false), so it applies to you.

Chrome unloads the page, and the reference to clickHandler can get lost. On the contrary, Event page mechanism ensures that if you registered an event with addListener the page will be loaded again, addListener applied again and then your listener executed.

So:

var clickHandler = function(e) {
  console.log('testing testing');
}

chrome.contextMenus.create({
  "title": "Click Me",
  "contexts": ["page", "selection", "image", "link"]
});

// Must be synchronously called on event page load,
//   for instance in the top level code
chrome.contextMenus.onClicked.addListener(clickHandler);

本文标签: javascriptchromecontextMenus onclick doesn39t do anythingStack Overflow