Chrome 扩展上下文菜单未显示
Chrome extension context menu not showing up
我正在尝试将上下文菜单项添加到 chrome 应用程序,但它根本没有显示。我读过的所有内容似乎都表明我在这里做的是正确的事,但显然我不是。
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.com/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"
]
}
没有记录错误或类似错误。该项目根本没有出现。我在这里错过了什么?
您在 contextMenu
文档中遗漏了一个 small note:
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
你确实有一个 Event page ("persistent": false
),所以它适用于你。
Chrome 卸载页面,并且对 clickHandler
的引用可能会丢失。相反,事件页面机制确保如果您使用 addListener
注册事件,页面将再次加载,再次应用 addListener
然后执行您的侦听器。
所以:
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);
我正在尝试将上下文菜单项添加到 chrome 应用程序,但它根本没有显示。我读过的所有内容似乎都表明我在这里做的是正确的事,但显然我不是。
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.com/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"
]
}
没有记录错误或类似错误。该项目根本没有出现。我在这里错过了什么?
您在 contextMenu
文档中遗漏了一个 small note:
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
你确实有一个 Event page ("persistent": false
),所以它适用于你。
Chrome 卸载页面,并且对 clickHandler
的引用可能会丢失。相反,事件页面机制确保如果您使用 addListener
注册事件,页面将再次加载,再次应用 addListener
然后执行您的侦听器。
所以:
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);