zequi
zequi

Reputation:

Select text and hit enter to execute a Chrome extension?

I made an google extension a while ago that searches selected text, putting it within quotation marks so google searches for that exact phrase.

I was able to give it a right-click entry to execute it and and to put a button nearby the address bar as well but for now I can't find a way to execute it with a keyboard shortcut (just Enter, in this case)

Now, this is what I want it to do:

Thanks for your time and sorry about my precarious English.

Upvotes: 1

Views: 846

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

You can use chrome.commands API: https://developer.chrome.com/extensions/commands.html

I doubt you can use "Enter" key, but you can use other combination:

Supported keys: A-Z, 0-9, Comma, Period, Home, End, PageUp, PageDown, Insert, Delete, Arrow keys (Up, Down, Left, Right) and the Media Keys (MediaNextTrack, MediaPlayPause, MediaPrevTrack, MediaStop).

Note: All key combinations must include either Ctrl or Alt...

Basic steps to use commands inside your extension:

  1. Define your command in manifest

    "commands": {
      "next-track": {
         "suggested_key": {
            "default": "MediaNextTrack"
         },
         "description": "Следующая песня"
      }
    }
    
  2. Subscribe to your command inside background page

    chrome.commands.onCommand.addListener(function(command) {
      if (command == "next-track") console.log('hello');
    });
    

You can check the working example at https://github.com/berlic/ymhk - this extension controls music-player with shortcuts.

Hope this helps, Konstantin

Upvotes: 1

Related Questions