Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use chrome.tabs.getCurrent to get the page object in a Chrome extension?

The code is meant to output the current tab object for the page the user is viewing to the console but it just outputs undefined. It's run from within a browser action page.

chrome.tabs.getCurrent( function(tab){
    console.log(tab);
} );

I've looked at the documentation and as far as I can tell the code seems to match what it says.

like image 629
Tom Avatar asked Jul 16 '11 15:07

Tom


2 Answers

The method getSelected() has been deprecated since Google Chrome 16 (but many articles in the official documentation had not yet been updated). Official message is here. To get the tab that is selected in the specified window, use chrome.tabs.query() with the argument {'active': true}. So now it should look like this:

chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
  console.log(tabs[0]);
});
like image 145
Konstantin Smolyanin Avatar answered Nov 12 '22 13:11

Konstantin Smolyanin


Try:

chrome.tabs.getSelected(null, function(tab){
    console.log(tab);
});
like image 22
serg Avatar answered Nov 12 '22 15:11

serg