I'm building a Google Chrome extension, and I'm trying to get the selected window in a popup. (I'm talking about the popup that shows when you click on the extension icon).
I tried to use the documentation, but I didn't understand it well. Specifically, I tried to use:
chrome.windows.getCurrent(function(w) {
chrome.windows.get(w.id,
function (response){
alert(response.location.href);
});
});
But it didn't work. Any ideas?
Thanks
(sorry if the English is bad).
1) have you added the "tabs" permission to the manifest?
{
"name": "My extension",
...
"permissions": ["tabs"],
...
}
2) It also looks like you should be using the tabs API and not the windows API if you want to know the current URL of the selected tab in the current Window
chrome.windows.getCurrent(function(w) {
chrome.tabs.getSelected(w.id,
function (response){
alert(response.url);
});
});
Related
I am developing a Chrome Extension for personal use and want to add a shortcut to it to activate to popup.html page (i.e. when I press the shortcut the popup.html page will show up).
For some extension I can easily do this by going to chrome://extensions/shortcuts page and assigning a shortcut against "Activate the extension" field.
But my extension is not listed there.
Do I need to add anything to the manifest.json file for my extension to appear in chrome://extensions/shortcuts page?
Thanks to Yu Zhou for sharing this link in his comment. It helped me to make the "Activate the extension" thing to work.
To make the extension available in chrome://extensions/shortcuts you need to add the following in manifest.json -
"commands": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+Shift+Y"
}
}
},
But doing only this doesn't make the shortcut assigned to actually work. For that you need to add a background.js page -
// in manifext.json
"background": {
"service_worker": "background.js"
}
// in background.js
chrome.action.onClicked.addListener((tab) => {
//TODO toggle dark mode in the tab
});
I am still unclear on how all these are making the popus.html to activate but it is working now.
P.S. this is for manifest version 3
The 'Best Answer' did not work for me, after a confusing 20 minutes I found out that all you need in your MV3 file is this:
"commands": {
"_execute_action": {
"suggested_key": {
"mac": "Shift+MacCtrl+G"
},
"description" : "Start the extension"
}
}
The catch was that I needed to fully Remove my extension and then Load unpacked for mine to work.
I have a browserAction extension testing on chrome 31.0.1650.57. I want to get active tab url when the user click on the extension icon, save it and use it later.
I am able to do that when using tabs permission. But this permission has a lot of power (that I don't need) and users are complaining about this.
I followed the article to switch to activeTab permission http://developer.chrome.com/extensions/activeTab but it only works when there is no popup. As you can see in the make_page_red example.
chrome.browserAction.onClicked.addListener(function(tab) {
// No tabs or host permissions needed!
console.log("★ tab.url", tab.url);
});
You can check by changing the make_page_red example manifest file to add a default popup like this:
{
"name": "Page Redder",
"description": "Make the current page red",
"version": "2.0",
"permissions": [
"activeTab"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_title": "Make this page red",
"default_popup": "popup.html" ←-------------------
},
"manifest_version": 2
}
Now because I have a default popup the browserAction.onClicked listener is not called anymore.
How can I get current tab url and still have my popup opening?
Thanks
browserAction.onClicked is not called anymore (as you noted), so just skip that:
popup.js:
chrome.tabs.query({active:true,currentWindow:true},function(tabArray){
console.log(tabArray[0].url);
});
And of course, have popup.html load popup.js with a script tag. (And I'm not sure what you're background script is doing.)
Edit:
In addition to the above, my extension has two other files. manifest.json is copied from yours, but has the background section deleted. The only other file is popup.html (from memory, so bear with me):
<html><body><script type="text/javascript" src="popup.js"></script></body></html>
To view the console, I click the browser action, which brings up a 50 pixel square window. Right click on that, "inspect element", and click console to view the url.
I have a created a desktop notification using google extension which works great:
icon = '';
var popup = window.webkitNotifications.createNotification(my notification');
icon, 'Awesome title', 'Click here to view more. ');
popup.show();
Is there a way to launch the actual google extension popup.html (as shown on the image below), when the user click on the the desktop notification?
Thanks.
Short answer is that you can't, but if you want to open the popup in a new tab instead, this is how you would do it:
Put this before you call show
popup.onclick = function() {
chrome.tabs.create({url : "popup.html"});
popup.cancel();
}
I wanted to add to the comment that Miscreant posted.
One approach that might work would be to setup a keyboard shortcut for
the pop up in the extension's manifest, then use an executable file to
artificially trigger that keyboard shortcut. See Native Messaging for
more info about how to communicate with an executable file from an
extension
This is how you set up a shortcut key that opens your extension.
...
"commands": {
"_execute_browser_action": {
"suggested_key": {
"default": "Ctrl+Shift+Y"
}
}
Here's the API on 'commands'.
I'm trying to work on a Chrome browser extension which does fun things on a contextmenu. The problem is, I can't get the JS files to load within the manifest.json content_scripts.
I don't receive an error, the files simply do not load.
The scripts are good, if I put them in my background page, they fire fine through that background page. The downside of that is it's restricted to only the background page.
{
"name": "Mini",
"description": "Mini",
"permissions": ["contextMenus","management","contentSettings","tabs","http://*/*","https://*/*","editable"],
"version": "0.1",
"manifest_version": 1,
"background_page": "mini.html",
"icons" : {"16" : "mini.png"},
"contexts" : ["link","tab","page"],
"content_scripts": [{"matches":["<all_urls>"],"js":["jquery172min.js","mini.js"]}]
}
I've tried all forms of matches, including "http://\*/\*", "https://\*/\*", "\*://\*/\*"
I've tried whitespaces, no whitespaces. I'm drawing a blank here.
Any help would be appreciated.
$(document).ready(function () {
alert("page is ready");
},true);
My apologies for that. This is a copy of the javascript/Jquery I'm using to test whether the extension has loaded or not. It's just a simple alert.
Content scripts cannot use the chrome.contextMenus API (in fact, these can only use some of the chrome.extension methods).
It's possible to apply match patterns to individual menu items, via chrome.contextMenus.create({title: 'Test', documentUrlPatterns: 'http://*/*'});.
Created menu items can also be modified (chrome.contextMenus.update) and removed (chrome.contextMenus.remove).
To communicate between a Content script and the background page, see Message passing.
I am currently writing a chrome extension so that I have a button on the bar and when clicked I wanted it to grab the URL of the currently selected tab and open a new tab as follows:
FixedURL/grabbedURL ex: lets say the currently opened page is "http://www.abc.com" and the fixed url I assign is "http://www.123.com" so the new URL I want to be opened will be http://www.123.com/http://www.abc.com
This way only the second part of URL opened in a new tab will change depending on the selected tab.
I already created a manifest.json and the icons, title, etc...
I then tried to use both background.html and popup.html but to no avail.
I also am kinda lost on the whole .js file thing and how to link all of them together.
The maximum I could achieve is having chrome open:
chrome://settings/extensions/obidbojnjbigokbpalmaacmgmecopond/title.url
when I clicked the button.
Here is my manifest.json
{
"name": "Circumvent",
"version": "1.0",
"description": "Opens new tab",
"browser_action": {
"default_icon": "111.png",
"default_title": "Circumvent!",
"popup": "main.html"
},
"icons": {
"48": "111.png"
},
"permissions": [
"tabs","notifications","http://*/*"
]
}
Any idea of what a sample code will look like and/or a brief clarification would be really awesome and very helpful!
Thanks in advance....
P.S. my first extension
You should add a background page and have it listen for clicks on your extension's browser action button:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({"url" : "http://www.123.com/" + tab.url})
});
(This code goes in your background page.)
You'll need to remove the popup line in your manifest, since the onClicked handler won't fire if there's a popup (and besides, you don't want a popup, you just need a button).