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.
Related
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
I'm trying to make a Google chrome extension that injects a script upon clicking the extension icon. However, i want the same script to be injected whenever I load/go to another page, without having to click on the extension icon again.
background.js
chrome.browserAction.onClicked.addListener(function (tab) {
if (tab.url.startsWith("https://")){
chrome.tabs.executeScript(tab.id, {
"file": "js/contentscript.js"
}, function () {
console.log("Script Executed .. "); // Notification on Completion
});
}
else{
alert('Error');
}
});
manifest.json i want to execute the script on browserAction so adding the script on content_scripts is not an option i would like to pursue
"permissions": ["tabs", "windows", "notifications", "activeTab"],
"version": "2.0",
"browser_action":
{
"name": "Click to activate extension",
"default_icon": "images/icon.png",
},
"background":
{
"scripts":["js/background.js"]
},
"icons":
{
"16": "images/icon16.png",
"48": "images/icon48.png",
"128": "images/icon128.png"
},
"content_scripts":
[{
"js": ["js/jquery-2.1.4.min.js"],
"matches": [ "https://*/*" ],
"run_at": "document_end"
}]
contenscript.js - only a sample script, but gets my point across
alert("here");
setTimeout(function(){window.open("www.google.com");});
P.S. This is my first time asking a question in stackoverflow, please be gentle with me. :D
You could listen to webNavigation.onCompleted event, which fires when a document, including the resources it refers to, is completely loaded and initialized.
Compared with tabs.onUpdated event, you could use event filters to restrict your event notifications. For this part, you could check my answer in this post Running an extension in background on page load/refresh for more details.
manifest.json
{
"name": "36735306",
"version": "1.0",
"description": "Your description here",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"web_accessible_resources": ["content.js"],
"permissions": [
"webNavigation",
"<all_urls>"
]
}
background.js
chrome.webNavigation.onCompleted.addListener(function(details) {
if(details.frameId === 0) {
chrome.tabs.executeScript(details.tabId, {"file": "content.js"});
}
});
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.
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.
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"
}
}