Chrome commands API
我已经从使用命令API的Chrome文档中加载了此示例扩展。
的manifest.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | { "name":"Sample Extension Commands extension", "description":"Press Ctrl+Shift+F (Command+Shift+F on a Mac) to open the browser action popup, press Ctrl+Shift+Y to send an event (Command+Shift+Y on a Mac).", "version":"1.0", "manifest_version": 2, "background": { "scripts": ["background.js"], "persistent": false }, "browser_action": { "default_popup":"browser_action.html" }, "commands": { "toggle-feature": { "suggested_key": {"default":"Ctrl+Shift+Y" }, "description":"Send a 'toggle-feature' event to the extension" }, "_execute_browser_action": { "suggested_key": { "default":"Ctrl+Shift+F", "mac":"MacCtrl+Shift+F" } } } } |
background.js
1 2 3 | chrome.commands.onCommand.addListener(function(command) { console.log('onCommand event received for message: ', command); }); |
非常简单,但是侦听器回调没有被触发 - 我在控制台中没有输出,也没有任何错误。 如果我使用其他API,例如标签,我的听众会被触发,它只是命令API对我不起作用。
评论者rsanchez提供了正确的答案:
Are you working with an unpacked extension? You need to remove and re-add the extension for suggested shorcut keys to be considered.
我遇到了同样的问题,这些建议没有帮助。 这是我想到的:因为您在