14

I've loaded this sample extension from Chrome docs which uses the commands API.

manifest.json

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

chrome.commands.onCommand.addListener(function(command) {
  console.log('onCommand event received for message: ', command);
});

Very simple, yet the listener callback is not getting triggered - I get no output in the console, nor any errors. If I use other API, for example tabs, my listeners are getting triggered as they should, it's just the commands API that doesn't work for me.

4

2 回答 2

16

评论者 rsanchez提供了正确答案:

您正在使用未打包的扩展程序吗?您需要删除并重新添加要考虑的建议快捷键的扩展名。

于 2013-12-23T16:09:45.503 回答
0

我遇到了同样的问题,这些建议没有帮助。这是我发现的:由于您在该background: {}部分中使用侦听器声明了脚本,因此它会记录到后台页面。您可以通过单击扩展名下方的“检查背景页面”来专门查看该日志chrome://extensions。那是侦听器登录的地方。

于 2017-03-20T04:30:08.283 回答