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.
Related
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.
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
Hi I have found other similiar questions on stackoverflow but none of them solved the purpose.
I want my chrome extension/app to be opened in a full tab like how POSTMAN extension is opened.
My manifest.json
{
"name": "Sample App",
"manifest_version": 2,
"version": "0.0.1",
"app": {
"background": {
"scripts": ["main.js"]
}
},
"icons": { "128": "icon.png" },
"permissions" : ["tabs" ]
}
My main.js (alias for background.js)
chrome.app.runtime.onLaunched.addListener(function() {
chrome.tabs.create({'url': chrome.extension.getURL('index.html')}, function(tab) {
alert("Hi");
});
});
index.html is the file i want to load on opening the new tab.
My first time responding here on stackoverflow, please be gentle...
I discovered that it's much easier to add "launch" : { "local_path" : "index.html" } within the manifest.json file. See my sample manifest file below.
{
"manifest_version" : 2,
"name": "Hello World!",
"description": "My first Chrome App.",
"version": "0.1",
"app": {
"launch" : {
"local_path" : "index.html"
}
},
"icons": { "16": "icon.png" }
}
Keep in mind that this example is very basic, it has been stripped of some unnecessary information such as a background script but it should accomplish what you want.
http://developer.chrome.com/apps/first_app.html
chrome.app.runtime.onLaunched is only for Chrome apps, not extensions. The code for your background page will automatically run when the Chrome browser starts, so you can start directly with chrome.tabs.create(...).
Also, you need to include index.html and any resource included in your extension that the page will use in a web_accesible_resources section in your manifest.
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).
I have a problem with my manifest.json for an Google Chrome extension. I want to load an script in the background. When I want to load the extension i get an syntax error, caused by the colon in line 10. I have looked in the Google Chrome Developer Documentation, but I don't find any helpfully information. Can you help me :D
{
"name": "Test",
"version": "1.0",
"manifest_version": 2,
"description": "Test",
"browser_action": {
},
"permissions": [["webRequest","webRequestBlocking",
"*://*.Test.com/*/*" ],
**"background": {**
"scripts": ["background.js"]
},
]
}
Your JSON is invalid and you really do have a syntax error. It looks like you tried to make the background part of the permissions array but here is what it should look like.
{
"name": "Test",
"version": "1.0",
"manifest_version": 2,
"description": "Test",
"permissions": [
"webRequest",
"webRequestBlocking",
"*://*.Test.com/*/*"
],
"background": {
"scripts": ["background.js"]
}
}
Keeping your JSON well formated will often help prevent mistakes like this. Good luck with your extension.