我需要热键 Alt + ]Alt + [ 。我有 manifest.json 像:

{
    ...
    "commands": {
        "nextTrack": {
            "suggested_key": {
                "default": "Alt+]"
            },
            "description": "Next track"
        },
        "previousTrack": {
            "suggested_key": {
                "default": "Alt+["
            },
            "description": "Previous track"
        },
        "toggle": {
            "suggested_key": {
                "default": "Alt+P"
            },
            "description": "Toggle pause"
        }
    },
    ...
}

当我启用我的扩展时,我得到:
Could not load extension from '~/project'.
Invalid value for 'commands[1].default': Alt+].

使用该热键的方法是什么?

最佳答案

只有大写字母 (A-Z) 和数字 (0-9) 是有效值,您可以通过查看 source code API 的 chrome.commands 看到。

如果要使用其他字符,请在每个绑定(bind) keydown 事件的页面中注入(inject)一个内容脚本:

document.addEventListener('keydown', function(event) {
    if (!event.ctrlKey && event.altKey && event.which === 80/*P*/) {
        // Dispatch a custom message, handled by your extension
        chrome.runtime.sendMessage('Alt+P');
    }
}, true); // <-- True is important

这种方法的缺点
  • 键盘焦点 必须 位于激活内容脚本的页面内。如果您在开发者工具、omnibar 等中,快捷方式将失败。
  • 即使您将 <all_urls> 用作 match pattern ,它也不适用于非 http(s)/file/ftp(s) 方案,例如 chrome:data:chrome-extension:about: 或 Chrome 网上商店。
  • 如果键盘布局不支持或使用不同的键代码,则在检测 [ 字符时可能会遇到问题。
  • 没有对自定义此快捷方式的内置支持(作为用户,请访问 chrome://extensions/ ,滚动到底部并单击“配置命令”以更改扩展程序的快捷方式)。

  • 我建议选择不同的快捷方式,或更改扩展程序的控制方式(例如,通过 page/browser action 弹出窗口)。

    关于google-chrome-extension - chrome.commands 中的括号时出现“无效值”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16034279/

    10-12 22:32
    查看更多