Oliver Nassar

Enabling Global Chrome/Web Extension Shortcuts

February 04, 2022

While debugging an issue with Bookee (keyboard shorcuts not working), I noticed that a friend of mine had an extension installed that allowed her to define a Global Shortcut.

At first, I had no clue what a Global Shortcut was (but I was curious). Upon researching more, I realized that a Global Shortcut allows users to initiate an extension command even when they're not in Chrome!

I thought this was ideal for Bookee, since I often want to go to a bookmarked URL even when I'm not using Chrome. Here are a few things I noticed/ran into:


Manifest changes requireed to allow for Global shortcuts

While exploring the manifest.json file for the Pocket Chrome Extension, I noticed that they define their shortcut combination differently. This is what I had for Bookee before:

"commands": {
    "_execute_action": {
        "suggested_key": {
            "default": "Ctrl+Shift+K"
        },
        "description": "Show Bookee"
    }
}

This is what I changed it to:

"commands": {
    "toggle-bookee-command": {
        "suggested_key": {
            "default": "Ctrl+Shift+K"
        },
        "description": "Toggle Bookee"
    }
}

Then, I naturally had to change the code/logic in the background script (or worker script if you're using version 3 of Chrome's manifest) to listen for that event.

By doing this, it made it possible for users (specifically, me) to change the scope of the keyboard shortcut to Global. Now, I can trigger Chrome to spring into action from any window on my computer.

For a little more context, the _execute_action property for the manifest.json file is kind of like a "default" property. Defining it will make a specific browser-action event available (which you can listen to using chrome.action.onClicked.addListener). But this has limitations: it's scope cannot be changed to Global, which means the keyboard combination will only be available within Chrome.

By defining the keyboard combination through it's own unique key (in this case toggle-bookee-command), the keyboard shortcut can be bound to the operating system (via the Global scope value).

Hope you find this helpful :)


Updates