14

コマンド API を使用する Chrome ドキュメントからこのサンプル拡張機能をロードしました。

マニフェスト.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);
});

非常に単純ですが、リスナーのコールバックがトリガーされていません。コンソールに出力もエラーもありません。タブなどの他の API を使用すると、リスナーが必要に応じてトリガーされます。コマンド API が機能しないだけです。

4

2 に答える 2

16

コメンターの rsanchezが正しい答えを提供しています。

アンパックされた拡張機能を使用していますか? 提案されたショートカット キーを考慮するには、拡張機能を削除して再度追加する必要があります。

于 2013-12-23T16:09:45.503 に答える