Steinn
Steinn

Reputation: 11

Getting current window on a popup (Google Chrome extension)

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).

Upvotes: 1

Views: 11308

Answers (1)

Kinlan
Kinlan

Reputation: 16597

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);
    });
});

Upvotes: 11

Related Questions