I want to create an extension for automatically logging into my servers. So I created a background page to check the current URL and if it conforms to my URL regex, I'll display the page action icon. On click of the page action I'm opening a popup with some fields. I need to get the currently opened URL and fill it in one of the fields in the popup(like when we click the standard bookmark page action, the URL gets automatically filled in the popup that opens). How can I do something like this in chrome extensions? I tried Message Passing from the background page to the popup html but it is not working. Is it possible to send messages like that? Also I tried setting onload for the popup html file but i noticed that it is not triggering. Please suggest a suitable method to deal with this scenario.
Asked
Active
Viewed 2.3k times
2 Answers
47
Use chrome.tabs.query
with the following parameters:
- queryInfo object:
active: true
- To get the active tablastFocusedWindow: true
- To select the active window
- callback function:
This function receives one argument: An array of matched tabs. Since only one window can be active, and one tab within this window, the array has only one element. This element is an object with theTab
signature.
Code snippet:
// Do NOT forget that the method is ASYNCHRONOUS
chrome.tabs.query({
active: true, // Select active tabs
lastFocusedWindow: true // In the current window
}, function(array_of_Tabs) {
// Since there can only be one active tab in one active window,
// the array has only one element
var tab = array_of_Tabs[0];
// Example:
var url = tab.url;
// ... do something with url variable
});
The activeTab
permission is sufficient for this to work.
-
Thank you for the answer Rob. But now the problem is that how do I get this running in the page action popup. I tried this code in a script tag in the head section of the popup html file but it is not getting executed when I open the popup. Anyway I can get this to run when the popup is opened? – Jophin Joseph May 03 '12 at 04:50
-
1@JophinJoseph Make sure that all conditions are met: 1. The `tab` [permission](http://code.google.com/chrome/extensions/manifest.html#permissions) should be set in the manifest file. 2. When using inline code, make sure that [`"manifest_version": 2`](http://stackoverflow.com/a/10417745/938089?communication-between-contentscript-js-and-chrome-extension) does not exist in the manifest file. [Inspect the popup](http://code.google.com/chrome/extensions/tut_debugging.html#inspect-popup) to see whether any error shows up. – Rob W May 03 '12 at 07:21
-
This works just fine with `"manifest_version": 2` in the manifest. – Soviut Aug 22 '13 at 09:43
-
Why does this use lastFocusedWindow over currentWindow? – Stuart P. Bentley Dec 29 '13 at 13:23
-
@StuartP.Bentley Doesn't matter in this case. They will both refer to to the window that triggered the page action dialog. However, see https://developer.chrome.com/extensions/windows.html#current-window for a note about the meaning of `currentWindow`. – Rob W Dec 29 '13 at 13:32
-
How to use this value? I am not able to capture the url or debug it because it is synchronous. Is there a way to show the URL in console or in alert box – TheTechGuy Jan 30 '21 at 20:22
4
You can also use promises for a cleaner way of retrieving a tab:
getCurrentTab().then(function(tab){
// Do something w/tab
});
function getCurrentTab(){
return new Promise(function(resolve, reject){
chrome.tabs.query({
active: true, // Select active tabs
lastFocusedWindow: true // In the current window
}, function(tabs) {
resolve(tabs[0]);
});
});
}

dipole_moment
- 5,266
- 4
- 39
- 55