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

I'm trying to add a context menu item to a chrome app and it's not showing up at all. Everyth

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745458795a4628611.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信