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 badges1 Answer
Reset to default 6You 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 forchrome.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
版权声明:本文标题:javascript - chrome.contextMenus `onclick` doesn't do anything - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745458794a2659227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论