使用Google Chrome扩展程序中的内容脚本来捕获按键事件

时间:2017-06-09 06:29:10

标签: javascript google-chrome google-chrome-extension

我正在尝试在构建chrome扩展程序时执行以下操作

我的清单文件

  "commands": {
      "detect" : {
          "_execute_browser_action": {
              "suggested_key": {
                "windows": "Ctrl+Shift+1",
                "mac": "Command+Shift+1",
                "chromeos": "Ctrl+Shift+1",
                "linux": "Ctrl+Shift+1"
              }
            },
            "_execute_page_action": {
              "suggested_key": {
                "default": "Ctrl+Shift+1",
                "windows": "Alt+Shift+1",
                "mac": "Command+Shift+1"
              }
          },
          "description": "Key press Event",
          "global": true
      }
     },
     "content_scripts": [
      {
        "matches": ["http://*/*","https://*/*"],
        "run_at": "document_end",
        "js": ["main.js"]
      }

我的main.js

chrome.commands.onCommand.addListener(function (command) {
    if (command === "detect") {
        alert("detect");
        console.log('Command:', command);
    }
});

所以,我打算在使用内容脚本注入js后捕获关键的新闻事件。但是,我无法捕获任何按键事件。

0 个答案:

没有答案