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.
Related
I have the following code in a background.js.
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
chrome.tabs.executeScript(tabId, {
file: 'inject.js'
});
} // if (changeInfo.status == 'complete') {
});
But when I change tabs the inject.js only runs in the active tab even if an inactive tab gets updated. In the inject.js I have an automated process running for a particular website and it's clicking through some pages looking for specific text. I want it to continue running on that one tab even if I open a new tab. But as soon as I change tabs the injection happens only on the active tab. I thought tab.tabId or tabId would be for the tab that was updated but it seems like it's always the active tab.
Is there a way to figure out what tab updated so I can pass the correct tabId to executeScript?
Here is what my manifest looks like:
{
"name": "Automated test",
"version": "0.0.1",
"manifest_version": 2,
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"browser_action": {
"default_title": "Test"
},
"permissions": [
"https://*/*",
"http://*/*",
"tabs",
"webNavigation"
]
}
So, #wOxxOm led me to what I needed. I did need a content-script and the reason why I think I wasn't seeing the results I was expecting is that I was failing to remove and re-add my extension. Sometimes it seems like it respects the changes made but maybe when you make changes to the manifest you need to remove and re-add the extension.
Once I did that it is correctly working on the inactive tab.
Hopefully this is something simple. I'm testing a simple Chrome Extension script and it appears it'll execute part of the script, but won't complete it. For example, if I add an alert() to the beginning of a script, it will execute the alert. But if I place it after anything calling the chrome DOM object, it won't execute. Here's an example:
Will execute alert
alert("Test");
chrome.webRequest.onCompleted.addListener(function (request) { });
Will not execute alert
chrome.webRequest.onCompleted.addListener(function (request) { });
alert("Test");
Am I missing something?
Here is my manifest:
{
"background": {
"persistent": true,
"scripts": [
"scripts/libs/jquery.1.11.2.min.js",
"scripts/background.js"
]
},
"browser_action": {
"default_icon": "resources/icon.19.png"
},
"icons": {
"48": "resources/icon.48.png"
},
"manifest_version": 2,
"name": "Test",
"permissions": [
"<all_urls>",
"webNavigation",
"webRequest",
"webRequestBlocking"
],
"version": "1.0"
}
You are missing debugging it yourself.
Go to chrome://extensions/ and load the Dev Tools for your background page. You will see an uncaught exception that stops execution.
For webRequest events, you must include a filter argument to the addListener function.
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.
At
http://developer.chrome.com/extensions/samples.html 2nd sample titled:
"A browser action with no icon that makes the page red"
I downloaded the 3 files (manifest, js, icon) and installed the extension sample.
When I run it I get a puzzle icon on the bar with a badge that increments every second.
When I click on the icon - nothing
When I click on the page - nothing
I also cannot see the supplied icon being used anywhere.
I don't know if this is a code issue or a usage issue.
manifest.json:
{
"name": "A browser action with no icon that makes the page red",
"version": "1.0",
"background": { "scripts": ["background.js"] },
"permissions": [
"tabs", "http://*/*"
],
"browser_action": {
"name": "Make this page red",
"icons": ["icon.png"]
},
"manifest_version": 2
}
background.js file
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(
null, {code:"document.body.style.background='red !important'"});
});
chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]});
var i = 0;
window.setInterval(function() {
chrome.browserAction.setBadgeText({text:String(i)});
i++;
}, 10);
This is a duplicate of How to make Google Chrome extension sample work?
Please see my answer below:
If you open up the sample zip... find backgrond.js... edit. Find the line that says:
null, {code:"document.body.style.background='red !important'"});
and remove the "!important". so it should read:
null, {code:"document.body.style.background='red'"});
That is it. just save and reload the extension, should work (unless the page has an !important flag set to the background).
I am afraid I don't know why the "!important" tag doesn't work but I have never been able to get it to work in an extension. Hopefully someone else here will be able to give an explanation and maybe a work around.
I want to check some values in the content of chrome browser page when it completely loaded
like that
if(document.body.innerText.indexOf("Cat") !=-1)
Where and when can I make my check? please give me an clear example
I read some thing about "Background.html" and "Content script" but I can't do
Register a content script in the manifest file at "run_at": "document_idle" (which is the default) and put your code in the content script file. Then the script will be run when the page is ready.
If you want to detect from the background page whether a page is completely loaded, use the chrome.webNavigation.onCompleted event and do whatever you want, such as calling chrome.tabs.executeScript to execute a content script. This method could be useful over the previous method if the list of URLs is dynamic or if the URL patterns cannot be described using the match pattern syntax.
chrome.webNavigation.onCompleted.addListener(function(details) {
chrome.tabs.executeScript(details.tabId, {
code: ' if (document.body.innerText.indexOf("Cat") !=-1) {' +
' alert("Cat not found!");' +
' }'
});
}, {
url: [{
// Runs on example.com, example.net, but also example.foo.com
hostContains: '.example.'
}],
});
The webNavigation and host permissions have to be set in manifest.json, e.g.:
{
"name": "Test",
"version": "1.0",
"background": { "scripts": ["background.js"] },
"permissions": [ "webNavigation", "*://*/*" ],
"manifest_version": 2
}