chrome.tabs.query supported from background pages, of course, if you have tabs permission. This is a supported route with Chrome 19.
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { var tab = tabs[0]; var url = tab.url; });
Note that currentWindow necessary because otherwise it returned the active tab for each window. This should ensure that you return only one tab.
Of course, keep in mind that this is an asynchronous API - you cannot access any data that it provides, except from the callback function. You can store values ββ(e.g. url here) in a higher scope, so another function can access it, but when making a callback, this will only provide the correct result.
(Below is my initial answer for posterity - this method is no longer needed, it requires an always-running background page, and getSelected() deprecated.)
First put this in background.html and make the myURL variable global:
var myURL = "about:blank";
Then run this in popup.html if you want to get the page url:
chrome.extension.getBackgroundPage().myURL;
So, if I were to appear inside the popup and I go to Google and click on your page or browser action, I will see http://google.com/webhp in the popup.
source share