How do I reassign popup's shortcut in chrome extension? - google-chrome-extension

This manifest.json works fine.
"commands": {
"_execute_browser_action": {
"suggested_key": {
"windows": "Alt+S",
"mac": "Alt+S",
"chromeos": "Alt+S",
"linux": "Alt+S"
}
}
},
How do I allow users to reassign shortcuts? I would like to reassign them right from popup. I have created input, which suppose to be filled with a single letter. This letter suppose to replace 'S' in the manifest. How do I save it into manifest or overwrite shortcut?

You can't integrate shortcut changing in your interface, but you can have a button that links to chrome://extensions/configureCommands

Related

How do you trigger a chrome extension via a keyboard shortcut?

I wrote a short chrome extension that works as intended when I click on it on my extension list.
Ie. my code runs onClicked (chrome.action.onClicked.addListener(...))
I would like to add a keyboard shortcut (eg. Ctrl+Shift+Q) but not sure how.
Any ideas?
For context: I don't really know javascript, I'm hacking this together as a helper for myself in my day to day work.
You can use a command.
First step is to add the command to the manifest.json file. I added an example down below it should look very similar to your, but the part you really need is the "commands": {} section. This tells chrome to pay attention to when a user hits "Ctrl+Shift+Q". Note you can add multiple commands, like in the example below.
{
"name": "Getting Started Example",
"description": "Build an Extension!",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"devtools_page": "devtools.html",
"commands": {
"name-of-command-passed-into-function": {
"suggested_key": "Ctrl+Shift+Q",
"description": "do somthing"
}
"shortcut2": {
"suggested_key": "Ctrl+Shift+A",
"description": "do somthing else"
}
},
"options_page": "options.html",
"permissions": ["storage", "activeTab", "scripting","tabs"],
}
Now when the user hits "Ctrl+Shift+Q" or "Ctrl+Shift+A", chrome will loos for some very specific code to run:
chrome.commands.onCommand.addListener((command,tab) => {
//do stuff here
});
Add this code and change the //do stuff here, to the code you want to run. The command property will simply be a string that matches what was types into the manifest, "name-of-command-passed-into-function" or "shortcut2". This is how you tell which command shortcut the user entered, but if you only need one, then delete the second one from the manifest file.

Chrome extension simple script not being injected

I'm trying my first steps in writing a minimal Chrome extension, and I cannot figure out why it does not execute my clientScript.js.
This is my manifest.json:
{
"name": "Sit back, relax and enjoy",
"version": "0.1",
"description": "Finds and clicks the +extra channel points button when it is available",
"permissions": [ "activeTab" ],
"content_scripts": [
{
"matches": [ "https://twitch.tv/*" ],
"js": [ "contentScript.js" ],
"run_at": "document_idle"
}
],
"manifest_version": 2
}
And this is the the script that I want executed on pages that match https://twitch.tv/*:
let intervalTimer
function sibareaen() {
const btn = document.querySelector('.tw-button.tw-button--success.tw-interactive')
if (btn) {
btn.click()
console.log('At your service - clicked the button for you!')
}
}
function toggleSibareaen(on) {
switch (on) {
case true:
intervalTimer = setInterval(sibareaen, 750)
break
case false:
clearInterval(intervalTimer)
break
default:
clearInterval(intervalTimer)
}
}
console.log('At your service - ready to click for you!')
toggleSibareaen(true)
I have both files in the same folder:
Also, I have properly "installed" the extension:
The console shows no errors related to the extension.
What am I missing?
Assuming you did reload the tab (the content scripts are injected only on initial tab load, see this for more info and a workaround), you're probably a victim of the infamous Google's decision to hide www in the address bar: if you enter the edit mode and select/copy the entire text (or simply double-click the address) you will see the site's URL is actually https://www.twitch.tv/ so your manifest.json has an incorrect URL pattern that doesn't match the real site.
Simply use a generalized pattern like "https://*.twitch.tv/*" that will match both https://www.twitch.tv/ and https://twitch.tv/ and any other subdomain(s).
P.S. as a debugging step, you can check if the content script is even injected by looking at the context selector in devtools console toolbar or in devtools -> Sources -> Content scripts panel. And if you want to restore the classic address bar behavior, see https://superuser.com/a/1498561

Sublime Text 3 FileBrowser plugin: how to open in new tab by default

I am using the Sublime Text 3 FileBrowser for file navigation on keyboard.
While usually I open files in new tabs, with FileBrowser, it opens
On the official documentation (https://packagecontrol.io/packages/FileBrowser), it says:
Open all marked items in new tabs ⌘+enter / ctrl+enter
So when I open a file on 'Enter', it will open in the same split pane as the
I want to change this behavior
So I go to Preferences -> Package Settings -> FileBrowser ->
Settings - Default
Key Bindings - Default (Common)
to look for options I can override in user settings. However, nothing seems right.
Somehow found an answer while posting the question, so I throw it out first and look for any better ways from the community.
By setting an open directory command in the Key Bindings, it reverts to opening in a new tab as by default.(I set it as a user setting for easier management: Preferences -> Package Settings -> FileBrowser -> Key Bindings - User)
(from the official package page: https://packagecontrol.io/packages/FileBrowser)
[
{
"keys": ["f1"],
"command": "dired",
"args": {
"immediate": true,
"single_pane": true,
"other_group": "left",
}
}
]
Of course, you can bind the "dired" command to any other suitable key, and tweak other behaviors such as putting it left or right.
One thing to note is that this must not be used with
{
"keys": ["enter"],
"command": "dired_select", "args": {"and_close": true},
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.dired" },
{ "key": "setting.dired_rename_mode", "operand": false }
]
}
Maybe there are ways to tweak around, so I can both open in new tab on "Enter", and let the browser close automatically after selection. For now, I'm settled for closing it with F1.

Chrome Extension - using commands (keyboard shortcuts) with num pad

I'm trying to implement keyboard shortcuts into a Chrome extension.
I have managed this, but I'd like to have a keyboard shortcut that makes use of the num pad rather than the numbers along the top (well, in addition to).
Below is my manifest and script.
Perhaps this is a limitation of Chrome commands.
manifest.json
{
"name": "Shortcut Test",
"version": "1",
"manifest_version": 2,
"background": {"scripts": ["background.js"]},
"commands": {
"shortcut_test": {
"suggested_key": {
"default": "Ctrl+0",
"windows": "Ctrl+0"
},
"description": "A test shortcut"
}
}
}
background.js
chrome.commands.onCommand.addListener(function(command){
console.log(command);
});
Yep, commands currently has a very limited key selection. Just normal letters and numbers.

«Invalid value» error while brackets in chrome.commands

I need hotkeys Alt + ] and Alt + [. I have manifest.json like:
{
...
"commands": {
"nextTrack": {
"suggested_key": {
"default": "Alt+]"
},
"description": "Next track"
},
"previousTrack": {
"suggested_key": {
"default": "Alt+["
},
"description": "Previous track"
},
"toggle": {
"suggested_key": {
"default": "Alt+P"
},
"description": "Toggle pause"
}
},
...
}
When I enable my extension I get:
Could not load extension from '~/project'.
Invalid value for 'commands[1].default': Alt+].
What is way to use that hotkeys?
Only uppercase letters (A-Z) and digits (0-9) are valid values, as you can see by looking at the source code of the chrome.commands API.
If you want to use other characters, inject a content script in every page which binds a keydown event:
document.addEventListener('keydown', function(event) {
if (!event.ctrlKey && event.altKey && event.which === 80/*P*/) {
// Dispatch a custom message, handled by your extension
chrome.runtime.sendMessage('Alt+P');
}
}, true); // <-- True is important
Disadvantages of this method
Keyboard focus must be within a page where the content script is activated. The shortcut will fail if you're inside the Developer tools, omnibar, etc.
Even if you use <all_urls> as a match pattern, it won't work on non http(s) / file / ftp(s) schemes like chrome:, data:, chrome-extension:, about: or the Chrome Web store.
You can run into problems with detecting the [ character, if the keyboard layout does not support it, or uses a different key code.
There's no built-in support for customizing this shortcut (as a user, visit chrome://extensions/, scroll to the bottom and click on "Configure commands" to change the extension's shortcut).
I suggest to pick a different shortcut, or change the way how your extension is controlled (through a page / browser action popup, for instance).

Resources