odbhut.shei.chhele
odbhut.shei.chhele

Reputation: 6234

I cannot make "commands" in chrome extension work

I am trying to write a chrome extension. I am facing some problems. I am unable to make the commands in chrome extension work. Below is given the code that I have written. Here is my manifest.json file.

{
    "manifest_version": 2,
    "name": "Test",
    "description": "This is just a test",
    "version": "1.0",
    "browser_action": {
        "default_icon": "icon.png"
    },
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["content.js"]
        }
    ],
    "commands": {
        "toggle-feature-foo": {
            "suggested_key": {
                "default": "Ctrl+Shift+1",
                "mac": "Command+Shift+1"
            },
            "description": "Show Alert"
        }
    }
}

Here is the content.js file.

alert("This is just a test 3");
chrome.commands.onCommand.addListener(function(command) {
    alert('Command:', command);
});

The problem: I can see the first alert. But when I press Ctrl+Shift+1 I am unable to see the second alert. What am I doing wrong?

Upvotes: 0

Views: 177

Answers (1)

Siddharth
Siddharth

Reputation: 7156

Content Script cannot use chrome.* APIs, with the exception of:

  1. extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest ) https://developer.chrome.com/extensions/extension#method-getURL
  2. i18n https://developer.chrome.com/extensions/i18n
  3. runtime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage ) https://developer.chrome.com/extensions/runtime
  4. storage https://developer.chrome.com/extensions/storage

Solution:

Add your code in background page or use message passing https://developer.chrome.com/extensions/messaging

Upvotes: 2

Related Questions