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

javascript - chrome.contextMenus `onclick` doesn't do anything - Stack Overflow

programmeradmin0浏览0评论

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);
发布评论

评论列表(0)

  1. 暂无评论