Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 views
in Technique[技术] by (71.8m points)

javascript - Chrome extension error with 'onCommand'

I'm getting the error "Uncaught TypeError: Cannot read property 'onCommand' of undefined" while running a Chrome Extension with the following content:

manifest.json:

{
  "name": "Test",
  "description": "Key command test.",
  "version": "1.0",
  "manifest_version": 2,
  "content_scripts": [ {
    "js": ["background_test.js"],
    "matches": [ "http://*/*", "https://*/*"]
  }],
  "commands": {
    "Ctrl+M": {
      "suggested_key": {
        "default": "Ctrl+M",
        "mac": "Command+M"
      },
      "description": "Ctrl+M."
    },
    "Ctrl-L": {
      "suggested_key": {
        "default": "Ctrl+L",
        "mac": "Command+L"
      },
      "description": "Ctrl+L"
    }
  }
}

background_test.js:

chrome.commands.onCommand.addListener(function(command) {
  if (command === "Ctrl+L") { 
    console.log("Ctrl-L successful.");
  }
  else if (command === "Ctrl+M") { 
    console.log("Ctrl+M successful.");
  }
}); 

All it's supposed to do is print "Ctrl-M successful" if Ctrl-M is pressed and print "Ctrl-L successful" if Ctrl-L is pressed.

This question appears to contain an answer to this problem, but I don't understand the answer and can't add a comment to ask for further explanation since I don't have enough reputation: "Is your onCommand listener defined in a content script? It probably won't work there; you need to include it in a background page or an action popup." How am I supposed to define onCommand in the background page?? I couldn't find anything on that anywhere, whether in the API or via Googling in general.

I also tried reloading the extension and manually inputting the keyboard shortcuts manually as suggested here, to no avail.

What am I missing here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The chrome.commands is not available by content_scripts (as defined in https://developer.chrome.com/extensions/content_scripts).

To get it working you can change your manifest to :

{
  "name": "Test",
  "description": "Key command test.",
  "version": "1.0",
  "manifest_version": 2,
  "permissions": [
    "<all_urls>"
  ],
  "background":
    {
    "scripts": ["background_test.js"],
    "persistent": true
    },

  "commands": {
    "Ctrl+M": {
      "suggested_key": {
        "default": "Ctrl+M",
        "mac": "Command+M"
      },
      "description": "Ctrl+M."
    },
    "Ctrl+L": {
      "suggested_key": {
        "default": "Ctrl+L",
        "mac": "Command+L"
      },
      "description": "Ctrl+L"
    }
  }
}

In addition Ctlr+L is not working (at least on Mac) as already used by chrome to get focus on adress bar.

The element will be visible in the console of the extension. To see it open chrome://extensions/ and click on the Inspect views: background page of your extension.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...