gpt4 book ai didi

javascript - 'onCommand' 的 Chrome 扩展错误

转载 作者:行者123 更新时间:2023-11-30 08:43:21 25 4
gpt4 key购买 nike

在运行具有以下内容的 Chrome 扩展程序时,我收到错误“未捕获的类型错误:无法读取未定义的属性‘onCommand’”:

list .json:

{
"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"
}
}
}

background_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 成功”,在按下 Ctrl-L 时打印“Ctrl-L 成功”。

This question似乎包含这个问题的答案,但我不明白答案,也无法添加评论来要求进一步解释,因为我没有足够的声誉:“是你的 onCommand 听众在内容脚本中定义?它可能在那里不起作用;您需要将它包含在背景页面或操作弹出窗口中。”我应该如何在后台页面中定义onCommand?我在任何地方都找不到任何关于它的信息,无论是在 API 中还是通过一般的谷歌搜索。

我还尝试重新加载扩展并按照建议手动输入键盘快捷键 here ,无济于事。

我在这里错过了什么?

最佳答案

content_scripts(如 https://developer.chrome.com/extensions/content_scripts 中所定义)无法使用 chrome.commands。

要使其正常工作,您可以将 list 更改为:

{
"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"
}
}
}

此外,Ctlr+L 不起作用(至少在 Mac 上),因为 chrome 已经使用它来将焦点放在地址栏上。

该元素将在扩展的控制台中可见。要查看它,请打开 chrome://extensions/并单击 Inspect views: background page of your extension。

关于javascript - 'onCommand' 的 Chrome 扩展错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24226994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com