1

I am working on extension, i want my chrome extension to toggle (show / hide) via command like say (mac: "CMD+SHIFT+9" or default: "Ctrl+Shift+9"); Though i have defined command in my manifest file:

{
  ......... 
  "commands": {
    "toggle-window": {
      "suggested_key": {
        "default": "Ctrl+Shift+9",
        "mac": "Command+Shift+9"
      },
      "description": "Toggle feature foo",
      "global": true
    },
    ........
 }

Now, What can i do in my backgroundScript.js to do that?

my backgroundScript.js is:

chrome.commands.onCommand.addListener(function(command) {
  if(command === "toggle-window") {
      console.log('Command:', command);
      /* Logic to show/hide will go here..*/
  }
});

How do i do do that? Thanks!

Sample Extension demo that have this "show/hide" feature implemented:

https://chrome.google.com/webstore/detail/meldium-browser-extension/fdocegmnehjgfhfjelhmaobjccoiklle

narainsagar
  • 1,079
  • 2
  • 13
  • 29
  • 1
    When I first saw this in the triage review queue, i thought this looked like an decent question...but as I read i saw this phrase: "Give me a code plz...". Don't do that. We're not here to write your code for you, but we're happy to help answer any specific questions you have. – JNYRanger Sep 21 '15 at 13:18
  • 1
    I'm guessing what you want by the behaviour of that extension...you don't need any logic in your background script for that. Just use "_execute_browser_action" instead of "toggle-window" in your manifest file, chrome will handle this functionality for you. Read [this](https://developer.chrome.com/extensions/commands). – cviejo Sep 21 '15 at 13:53
  • Thanks for the response though, I didn't want you to give me axact code what i mean was just give me basic idea, Though i am trying what you have described, and will let you know about it.. Thanks – narainsagar Sep 22 '15 at 04:25
  • I tried this "_execute_browser_action" in my manifest file. It's not working at my side. – narainsagar Sep 22 '15 at 04:52
  • Also I am testing it locally in the Dev Mode! – narainsagar Sep 22 '15 at 05:25
  • @CViejo I got it solved! You were right that using "_execute_browser_action", chrome will handle this functionality for us, nicely... Thanks buddy! – narainsagar Sep 22 '15 at 05:48

1 Answers1

0

After a long research i finally got it solved.. First of all, thanks to @CViejo, He gave me a hint to the problem:

"Just use "_execute_browser_action" instead of "toggle-window" in your manifest file, chrome will handle this functionality for you.

I updated my manifest.js file like this as @CViejo said:

{
  ......... 
  "commands": {
    "_execute_browser_action": {
      "suggested_key": {
        "default": "Ctrl+Shift+9",
        "mac": "Command+Shift+9"
      }
    }
    ........
 }

then, i reloaded my extension but it still didn't worked.. then i just find keyboard shortcuts box on the bottom of the extensions page, i read about it online, it basically helps us "to verify that suggested keys are actually set there on Keyboard shortcuts box that are defined in manifest file". i checked the keyboard shortcuts box the key was not set, even if the key was available and defined in the manifest.

And then from the stack overflow, i found this one (that was major mistake): https://stackoverflow.com/a/25654514/5228251

As you can see in the source code here: https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/extensions/api/commands/command_service.cc&l=303&sq=package:chromium&rcl=1409677023

The key binding update is only run when OnExtensionWillBeInstalled callback is triggered.

So you need to uninstall and reinstall your local extension to see the default keyboard command appear in : chrome://extensions/configureCommands

I just did uninstalled an re-installed my unpacked extension and checked that default keyboard command appear in chrome://extensions/configureCommands

And it started worked in my extension...

Many Thanks to @CViejo and @Stephane Brillant...

May this also help others.....

Community
  • 1
  • 1
narainsagar
  • 1,079
  • 2
  • 13
  • 29
  • I’m trying that last uninstall/reinstall with an unpacked extension right now and getting nothing. I wonder if this has changed (or if I’m missing something else)? – MrColes May 09 '16 at 01:33
  • *UPDATE:* looks like I was potentially falling into this issue (tried Ctrl+Shift+P)—“*Certain Chrome shortcuts (e.g. window management) always take priority over Extension Command shortcuts and can not be overwritten.” https://developer.chrome.com/extensions/commands — it works when I use a different key. – MrColes May 09 '16 at 01:44
  • @MrColes Yes, you're absolutely right. "Your custom shortcut needs to be the different one from the chrome's default (pre-defined) ones" ! :thumbsup – narainsagar May 09 '16 at 10:33