8

Chrome Extension: I am looking for a way to assign global keyboard shortcuts that can be invoked even when the current tab has no content (and hence no content script). Some examples of such tabs: 'new tab' tabs, chrome://extensions tabs, 'page not loaded' tabs, etc Use cases for such a requirement are operations like close tab, go to next/prev tab, etc.

The chrome.commands api allows one to do this. However, there seems to be no way for the user to configure these keyboard shortcuts, which is something I'd really want my extension to allow.

Is there any way to get configurable keyboard shortcuts that don't need a content script?

Himanshu P
  • 9,586
  • 6
  • 37
  • 46
  • Thanks to your question I was able to create https://chrome.google.com/webstore/detail/global-new-tab-shortcut/dcngopenklmnfdlfocljeaokkhcplong?hl=en-US which opens a new chrome tab from anywhere :-) – w00t Jun 18 '14 at 08:10

1 Answers1

6

The shortcut for chrome.commands cannot be changed by the extension, but it is configurable by the user. This interface is built-in and offered by Chrome itself.

Visit the extensions page, scroll down and click on the Configure commands link. A box will be shown, where the user can set the preferred shortcut:

The code for the "Remap shortcut" extension is posted in this answer.

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 1
    Thanks, that is really neat! Another related question: can one give a link within the extensions options.html page to get to this interface (so that this feels more integrated with the extension's settings)? – Himanshu P Apr 29 '13 at 14:58
  • @HimanshuPokhariya You can't link to `chrome://extensions/`, but you can use `chrome.tabs.create({url:'chrome://extensions/'});` to open the page (instruct the user to scroll down before opening the page though). – Rob W Apr 29 '13 at 15:01
  • Hmm, thanks. I guess that's asking for too much, but I suppose there is no way to link to the 'Configure commands' link directly? – Himanshu P Apr 29 '13 at 15:03
  • Hmm, it seems to simply open the extensions page... nothing more – Himanshu P Apr 29 '13 at 15:31
  • @HimanshuPokhariya Ah. The anchor only works if the page is already open, because the content is dynamically generated. You have to tell the user to scroll down (a screenshot / screencast might help). – Rob W Apr 29 '13 at 15:40
  • Damn. But I guess I was expecting too much. Thanks for help anyway! – Himanshu P Apr 29 '13 at 15:48
  • Can an extension programmatically access (read only) the `chrome.commands` shortcuts that belong to it, and find what the user specified values of those shortcuts are? – Himanshu P Apr 29 '13 at 16:45
  • 1
    [`chrome.commands.getAll`](https://developer.chrome.com/extensions/commands.html#method-getAll) yields a list of [commands](https://developer.chrome.com/extensions/commands.html#type-Command), each ought have a shortcut property. – Rob W Apr 29 '13 at 16:47
  • 5
    Now you can point your user to this dialog with chrome://extensions/configureCommands – Mik Jan 23 '15 at 21:33