How to give a Chrome extension permissions to "chrome.commands"? [duplicate] - javascript

I'm getting the error "Uncaught TypeError: Cannot read property 'onCommand' of undefined" while running a Chrome Extension with the following content:
manifest.json:
{
"name": "Test",
"description": "Key command test.",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [ {
"js": ["background_test.js"],
"matches": [ "http://*/*", "https://*/*"]
}],
"commands": {
"Ctrl+M": {
"suggested_key": {
"default": "Ctrl+M",
"mac": "Command+M"
},
"description": "Ctrl+M."
},
"Ctrl-L": {
"suggested_key": {
"default": "Ctrl+L",
"mac": "Command+L"
},
"description": "Ctrl+L"
}
}
}
background_test.js:
chrome.commands.onCommand.addListener(function(command) {
if (command === "Ctrl+L") {
console.log("Ctrl-L successful.");
}
else if (command === "Ctrl+M") {
console.log("Ctrl+M successful.");
}
});
All it's supposed to do is print "Ctrl-M successful" if Ctrl-M is pressed and print "Ctrl-L successful" if Ctrl-L is pressed.
This question appears to contain an answer to this problem, but I don't understand the answer and can't add a comment to ask for further explanation since I don't have enough reputation: "Is your onCommand listener defined in a content script? It probably won't work there; you need to include it in a background page or an action popup." How am I supposed to define onCommand in the background page?? I couldn't find anything on that anywhere, whether in the API or via Googling in general.
I also tried reloading the extension and manually inputting the keyboard shortcuts manually as suggested here, to no avail.
What am I missing here?

The chrome.commands is not available by content_scripts (as defined in https://developer.chrome.com/extensions/content_scripts).
To get it working you can change your manifest to :
{
"name": "Test",
"description": "Key command test.",
"version": "1.0",
"manifest_version": 2,
"permissions": [
"<all_urls>"
],
"background":
{
"scripts": ["background_test.js"],
"persistent": true
},
"commands": {
"Ctrl+M": {
"suggested_key": {
"default": "Ctrl+M",
"mac": "Command+M"
},
"description": "Ctrl+M."
},
"Ctrl+L": {
"suggested_key": {
"default": "Ctrl+L",
"mac": "Command+L"
},
"description": "Ctrl+L"
}
}
}
In addition Ctlr+L is not working (at least on Mac) as already used by chrome to get focus on adress bar.
The element will be visible in the console of the extension. To see it open chrome://extensions/ and click on the Inspect views: background page of your extension.

Related

onCommand.addListener not working in my chrome extension

I am currently developing a chrome extension, only, I am facing a problem since onCommand.addListener is not working in my background.js file. My manifest.json file looks like this:
{
"manifest_version": 3,
"name": "Copy",
"version": "1.0.0",
"permissions": [
"storage",
"activeTab",
"scripting"
],
"background": {
"service_worker": "js/background.js"
},
"commands": {
"run-foo": {
"suggested_key": {
"default": "Ctrl+Shift+Y",
"mac": "Command+Shift+Y"
},
"description": "Run \"foo\" on the current page."
},
"_execute_action": {
"suggested_key": {
"windows": "Ctrl+Shift+Y",
"mac": "Command+Shift+Y",
"chromeos": "Ctrl+Shift+U",
"linux": "Ctrl+Shift+J"
}
}
}
}
And my background.js file in the js folder looks like this:
console.log('test');
chrome.commands.onCommand.addListener(function(command){
if(command === 'run-foo'){
sendResponse({data: window.getSelection().toString()});
console.log('copiƩ !!!');
let copieText = 'je suis le texte copiƩ';
}
});
chrome.commands.onCommand.addListener(function(command) {
console.log('onCommand event received for message: ', command);
});
But my console only returns the test, even if I press Ctrl-C several times.
Thank you in advance

Detect user's keyboard shortcut in a chrome extension

I am currently developing a chrome extension. Only, here I am confronted with a problem:
I would like my extension to detect when the user performs a keyboard shortcut and that triggers a JS function. For that, my manifest.json file looks like this:
{
"manifest_version": 3,
"name": "name",
"version": "1.0.0",
"description": "description.",
"icons":{
"16": "img/logo16.png",
"48": "img/logo48.png",
"128": "img/logo128.png"
},
"permissions": [
"storage",
"activeTab",
"scripting"
],
"background": {
"service_worker": "js/background.js"
},
"commands": {
"Restart App": {
"suggested_key": {
"default": "Ctrl+Shift+6",
"mac": "Command+Shift+6"
},
"description": "Restart my app (Debugging)"
}
}
}
My background.js file located in the js folder, contains this:
chrome.commands.onCommand.addListener(function (command)
{
if (command == "Restart App")
{
chrome.runtime.reload();
};
alert('app restart success !');
});
Only, when I launch the extension and execute the keyboard shortcut, nothing happens, no error is reported.
Would you have any ideas, thank you in advance.

Chrome Keyboard shortcuts

I am trying to get a keyboard shortcut to work and at this point I am just testing to see that pressing the shortcut works. So currently what I have it do is send a message to the background page that it is working but the key press never registers. I was wondering how to address this issue.
manifest.json
{
"manifest_version": 2,
"name": "My Cool Extension",
"version": "0.3.1.5",
"description":"User can enter in wepage and press button to open webpage in new tab.",
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery-2.1.4.min.js", "content.js"]
}
],
"browser_action": {
"default_icon": "arrow.png",
"default_popup":"popup.html"
},
"permissions": [
"tabs",
"storage"
],
"icons":{
"128":"arrow.png"
},
"commands": {
"openSavedTab": {
"suggested_key": {
"default": "Ctrl+Shift+Y",
"mac": "Command+Shift+Y"
},
"description": "Opens saved tab"
}
}
}
tab_shortcuts.js
chrome.commands.onCommand.addListener(function(command) {
chrome.tabs.update({}, function(tab) {
if (command == 'toggle-pin-tab')
chrome.extension.getBackgroundPage().console.log("Shortcut is functional");
alert("working");
});
});
Thank you wOxxOm for your help. You were right when you said the issue was with a conflicting extension with the same hotkey assigned. Once I disabled the extension my extension worked properly.

Chrome extension error with 'onCommand'

I'm getting the error "Uncaught TypeError: Cannot read property 'onCommand' of undefined" while running a Chrome Extension with the following content:
manifest.json:
{
"name": "Test",
"description": "Key command test.",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [ {
"js": ["background_test.js"],
"matches": [ "http://*/*", "https://*/*"]
}],
"commands": {
"Ctrl+M": {
"suggested_key": {
"default": "Ctrl+M",
"mac": "Command+M"
},
"description": "Ctrl+M."
},
"Ctrl-L": {
"suggested_key": {
"default": "Ctrl+L",
"mac": "Command+L"
},
"description": "Ctrl+L"
}
}
}
background_test.js:
chrome.commands.onCommand.addListener(function(command) {
if (command === "Ctrl+L") {
console.log("Ctrl-L successful.");
}
else if (command === "Ctrl+M") {
console.log("Ctrl+M successful.");
}
});
All it's supposed to do is print "Ctrl-M successful" if Ctrl-M is pressed and print "Ctrl-L successful" if Ctrl-L is pressed.
This question appears to contain an answer to this problem, but I don't understand the answer and can't add a comment to ask for further explanation since I don't have enough reputation: "Is your onCommand listener defined in a content script? It probably won't work there; you need to include it in a background page or an action popup." How am I supposed to define onCommand in the background page?? I couldn't find anything on that anywhere, whether in the API or via Googling in general.
I also tried reloading the extension and manually inputting the keyboard shortcuts manually as suggested here, to no avail.
What am I missing here?
The chrome.commands is not available by content_scripts (as defined in https://developer.chrome.com/extensions/content_scripts).
To get it working you can change your manifest to :
{
"name": "Test",
"description": "Key command test.",
"version": "1.0",
"manifest_version": 2,
"permissions": [
"<all_urls>"
],
"background":
{
"scripts": ["background_test.js"],
"persistent": true
},
"commands": {
"Ctrl+M": {
"suggested_key": {
"default": "Ctrl+M",
"mac": "Command+M"
},
"description": "Ctrl+M."
},
"Ctrl+L": {
"suggested_key": {
"default": "Ctrl+L",
"mac": "Command+L"
},
"description": "Ctrl+L"
}
}
}
In addition Ctlr+L is not working (at least on Mac) as already used by chrome to get focus on adress bar.
The element will be visible in the console of the extension. To see it open chrome://extensions/ and click on the Inspect views: background page of your extension.

how to set associate a hotkey to my chrome extension browser_action?

I have set up a hotkey in my manifest.json file, and a bogus listener in my background.js
If i hit the hotkey combination, it works.
Now, i'm missing how to actually do what i want it to do: launch the extension's browser_action : open the popup.html (which is a search your bookmarks input field).
I've been looking for a method like chrome.trigger('browserAction'); Perhaps i'm missing something really obvious...
my background.js
chrome.commands.onCommand.addListener(function(command) {
alert('Command:'+ command);
});
manifest.json (extract)
"commands": {
"browser_action": {
"suggested_key": {
"default": "Ctrl+Shift+U",
"mac": "Alt+Shift+U"
},
"description": "Opens the extension"
}
},
"browser_action": {
"default_title": "YURLS",
"default_icon": "icon16.png",
"default_popup": "popup.html"
},
ah. Simply changing the command name to the magic word _execute_browser_action in the manifest.json file did the trick.
"commands": {
"_execute_browser_action": {
"suggested_key": {
"default": "Ctrl+Shift+U",
"mac": "Alt+Shift+U"
},
"description": "Opens Yurl"
}
}

Categories

Resources