Javascript Chrome扩展错误,带有';onCommand';
运行包含以下内容的Chrome扩展时,我收到错误“Uncaught TypeError:无法读取未定义的属性'onCommand'”: manifest.json:Javascript Chrome扩展错误,带有';onCommand';,javascript,google-chrome,google-chrome-extension,keyboard-shortcuts,Javascript,Google Chrome,Google Chrome Extension,Keyboard Shortcuts,运行包含以下内容的Chrome扩展时,我收到错误“Uncaught TypeError:无法读取未定义的属性'onCommand'”: manifest.json: { "name": "Test", "description": "Key command test.", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "js": ["background_test.js"], "
{
"name": "Test",
"description": "Key command test.",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [ {
"js": ["background_test.js"],
"matches": [ "http://*/*", "https://*/*"]
}],
"commands": {
"Ctrl+M": {
"suggested_key": {
"default": "Ctrl+M",
"mac": "Command+M"
},
"description": "Ctrl+M."
},
"Ctrl-L": {
"suggested_key": {
"default": "Ctrl+L",
"mac": "Command+L"
},
"description": "Ctrl+L"
}
}
}
背景_test.js:
chrome.commands.onCommand.addListener(function(command) {
if (command === "Ctrl+L") {
console.log("Ctrl-L successful.");
}
else if (command === "Ctrl+M") {
console.log("Ctrl+M successful.");
}
});
它所要做的就是在按下Ctrl-M时打印“Ctrl-M successful”,在按下Ctrl-L时打印“Ctrl-L successful”
似乎包含了此问题的答案,但我不理解答案,也无法添加注释以要求进一步解释,因为我没有足够的声誉:“您的onCommand
侦听器是否在内容脚本中定义?它可能在那里不起作用;您需要将其包含在背景页或操作弹出窗口中。”我应该如何在后台页面中定义onCommand
??我在任何地方都找不到这方面的任何东西,无论是在API中还是通过谷歌搜索
我还尝试重新加载扩展,并按照建议手动输入键盘快捷键,但没有成功
这里缺少什么?内容脚本(如中所定义)无法使用chrome.commands 要使其正常工作,您可以将清单更改为:
{
"name": "Test",
"description": "Key command test.",
"version": "1.0",
"manifest_version": 2,
"permissions": [
"<all_urls>"
],
"background":
{
"scripts": ["background_test.js"],
"persistent": true
},
"commands": {
"Ctrl+M": {
"suggested_key": {
"default": "Ctrl+M",
"mac": "Command+M"
},
"description": "Ctrl+M."
},
"Ctrl+L": {
"suggested_key": {
"default": "Ctrl+L",
"mac": "Command+L"
},
"description": "Ctrl+L"
}
}
}
{
“名称”:“测试”,
“说明”:“按键命令测试”,
“版本”:“1.0”,
“清单版本”:2,
“权限”:[
""
],
“背景”:
{
“脚本”:[“background_test.js”],
“持久”:正确
},
“命令”:{
“Ctrl+M”:{
“建议的_键”:{
“默认值”:“Ctrl+M”,
“mac”:“命令+M”
},
“说明”:“Ctrl+M”
},
“Ctrl+L”:{
“建议的_键”:{
“默认值”:“Ctrl+L”,
“mac”:“命令+L”
},
“说明”:“Ctrl+L”
}
}
}
此外,Ctlr+L不工作(至少在Mac上),因为chrome已经使用它来获得地址栏的焦点
元素将在扩展的控制台中可见。看它打开chrome://extensions/ 然后点击你分机的“查看视图:背景”页面。哦,天哪,我永远也想不到这一点(很明显)。这起作用了。非常感谢。