0

As the title says, after I load the extension, background loads (shown by alert message), but then onCommand doesn't work. I loaded the extension to Chrome and it worked only once (not the first time when I loaded the extension, but when I clicked the hot key).

I am using Canary.

Background.js

alert('a');
chrome.commands.onCommand.addListener(function (command) {
    alert('clicked');
    if (command === "toggle-feature") {
        chrome.tabs.query({}, function (tabs) {
            alert('tabs');
            chrome.tabs.executeScript(tabs[1].id, {"file": "content_script.js"});
        });
    }

});

manifest.json

{
    "manifest_version": 2,
    "name": "Extractor",
    "version": "1",
    "description": "Extract",
    "icons": {
        "16": "logo16.png",
        "48": "logo48.png",
        "128": "logo128.png"
    },
    "page_action": {
        "default_icon": {
            "16": "logo16.png",
            "48": "logo48.png",
            "128": "logo128.png"
        },
        "default_title": "Extractor"
    },
    "background": {
        "scripts": ["background.js"],
        "persistent": true
    },
    "permissions": [
        "tabs",
        "https://www.msn.com/*",
        "activeTab",
        "http://*/*",
        "https://*/*"    
    ],
    "commands": {
        "toggle-feature": {
            "suggested_key": {
                "default": "Ctrl+Shift+1",
                "windows": "Ctrl+Shift+2"
            },    
            "description": "Extract now"
        }
    }    
}

I tried to remove and change persistent value but with no luck.

I am guessing this is a more fundamental problem maybe with Chrome. I disabled cache in developer mode and also removed then installed the extension.

Makyen
  • 31,849
  • 12
  • 86
  • 121
user7403949
  • 19
  • 1
  • 3
  • 1
    Unless you're using a feature of Canary, I'd be developing on Chrome stable. It's very frustrating when a browser bug is stopping you. I'll take a deeper look at everything in a few minutes. – SethWhite Jan 12 '17 at 19:05
  • Are you trying to inject the *content_script.js* into the active tab when the hotkey command is pressed? – Makyen Jan 12 '17 at 19:09
  • What *exactly* is shown in the [various appropriate consoles for your extension](http://stackoverflow.com/a/38920982/3773011) when you load and execute your extension? – Makyen Jan 12 '17 at 19:15

1 Answers1

1

You are probably trying to inject content_script.js into the active tab in the current window. The list you are currently getting from chrome.tabs.query() will include all of the tabs that are open in all of the windows which are open. Your current code will try to inject that script into whatever tab is listed second in that list.

You should change your chrome.tabs.query() and chrome.tabs.executeScript() to something like:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    //The active tab in the current window with be tabs[0].
    chrome.tabs.executeScript(tabs[0].id, {"file": "content_script.js"});
});

I only tested this in the release version of Chrome, not Canary. If you still are having problems in Canary, I would suggest that you test on the release version of Chrome so that any problems that are Canary specific can be kept isolated from your extension. Once you have it working in the release version of Chrome, then try using Canary.

Makyen
  • 31,849
  • 12
  • 86
  • 121