Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

«Invalid value» error while brackets in chrome.commands

I need hotkeys Alt + ] and Alt + [. I have manifest.json like:

{
    ...
    "commands": {
        "nextTrack": {
            "suggested_key": {
                "default": "Alt+]"
            },
            "description": "Next track"
        },
        "previousTrack": {
            "suggested_key": {
                "default": "Alt+["
            },
            "description": "Previous track"
        },
        "toggle": {
            "suggested_key": {
                "default": "Alt+P"
            },
            "description": "Toggle pause"
        }
    },
    ...
}

When I enable my extension I get:

Could not load extension from '~/project'. 
Invalid value for 'commands[1].default': Alt+].

What is way to use that hotkeys?

like image 777
Vladislav Avatar asked Sep 05 '25 20:09

Vladislav


1 Answers

Only uppercase letters (A-Z) and digits (0-9) are valid values, as you can see by looking at the source code of the chrome.commands API.

If you want to use other characters, inject a content script in every page which binds a keydown event:

document.addEventListener('keydown', function(event) {
    if (!event.ctrlKey && event.altKey && event.which === 80/*P*/) {
        // Dispatch a custom message, handled by your extension
        chrome.runtime.sendMessage('Alt+P');
    }
}, true); // <-- True is important

Disadvantages of this method

  • Keyboard focus must be within a page where the content script is activated. The shortcut will fail if you're inside the Developer tools, omnibar, etc.
  • Even if you use <all_urls> as a match pattern, it won't work on non http(s) / file / ftp(s) schemes like chrome:, data:, chrome-extension:, about: or the Chrome Web store.
  • You can run into problems with detecting the [ character, if the keyboard layout does not support it, or uses a different key code.
  • There's no built-in support for customizing this shortcut (as a user, visit chrome://extensions/, scroll to the bottom and click on "Configure commands" to change the extension's shortcut).

I suggest to pick a different shortcut, or change the way how your extension is controlled (through a page / browser action popup, for instance).

like image 195
Rob W Avatar answered Sep 09 '25 07:09

Rob W