Reputation:
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:
If text is selected, hitting enter should execute my script.
If no text is selected, hitting enter should do nothing.
Thanks for your time and sorry about my precarious English.
Upvotes: 1
Views: 846
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:
Define your command in manifest
"commands": {
"next-track": {
"suggested_key": {
"default": "MediaNextTrack"
},
"description": "Следующая песня"
}
}
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