1

I am developing a Chrome Extension for personal use and want to add a shortcut to it to activate to popup.html page (i.e. when I press the shortcut the popup.html page will show up).

For some extension I can easily do this by going to chrome://extensions/shortcuts page and assigning a shortcut against "Activate the extension" field.

But my extension is not listed there.

Do I need to add anything to the manifest.json file for my extension to appear in chrome://extensions/shortcuts page?

th1rdey3
  • 4,176
  • 7
  • 30
  • 66
  • 1
    You need to define browser_action (MV2) or action (MV3) in manifest.json. – wOxxOm Sep 23 '21 at 14:11
  • 1
    I agree with wOxxOm's comment. For more information, you can also refer to [this blog](https://blog.shahednasser.com/how-to-add-keyboard-shortcuts-in-a-chrome-extension/): *Action commands map the shortcuts to your extension's action (if MV3) or browser action or page action (if MV2).* – Yu Zhou Sep 24 '21 at 07:42

2 Answers2

2

Thanks to Yu Zhou for sharing this link in his comment. It helped me to make the "Activate the extension" thing to work.

To make the extension available in chrome://extensions/shortcuts you need to add the following in manifest.json -

"commands": {
    "_execute_action": {
        "suggested_key": {
            "default": "Ctrl+Shift+Y"
        }
    }
},

But doing only this doesn't make the shortcut assigned to actually work. For that you need to add a background.js page -

// in manifext.json
"background": {
    "service_worker": "background.js"
}
// in background.js
chrome.action.onClicked.addListener((tab) => {
    //TODO toggle dark mode in the tab
});

I am still unclear on how all these are making the popus.html to activate but it is working now.

P.S. this is for manifest version 3

th1rdey3
  • 4,176
  • 7
  • 30
  • 66
  • 1
    Thanks for posting the solution for this issue. You can mark your answer as an accepted answer. It can help other community members in future in similar kind of issues. Thanks for your understanding. – Yu Zhou Sep 27 '21 at 01:52
  • Just to note that I don't think the `service_worker` part is necessary to just get your extension dialog to show. I was a bit confused by this too, there doesn't seem to be much information online about chrome extensions. – Paul Rooney May 26 '22 at 01:31
  • This didn't work for me. I'm migrating from MV2->MV3 and still not able to get the hotkey to invoke the extension to work. – Zafar Jun 01 '22 at 22:46
2

The 'Best Answer' did not work for me, after a confusing 20 minutes I found out that all you need in your MV3 file is this:

"commands": {
  "_execute_action": {
    "suggested_key": {
        "mac": "Shift+MacCtrl+G"
      },
    "description" : "Start the extension"
    }
}

The catch was that I needed to fully Remove my extension and then Load unpacked for mine to work.

Zafar
  • 1,897
  • 15
  • 33