45

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.

Sudarshan
  • 18,140
  • 7
  • 53
  • 61
Tom
  • 525
  • 1
  • 4
  • 8
  • 4
    Browser Action popup pages aren't part of any tab, so `tabs.getCurrent()` won't work. Instead, serg's answer with `tabs.getSelected()` should do the trick. – Chris McFarland Jul 18 '11 at 00:42

5 Answers5

117

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]);
});
Konstantin Smolyanin
  • 17,579
  • 12
  • 56
  • 56
  • this always returns 0 length array in the callback( – SuperUberDuper Jul 06 '17 at 11:16
  • what if in one desktop I have 2 chrome windows open, each show me a tab, in this case is this possible to have >1 active tabs? – lsheng Jul 15 '17 at 07:08
  • 1
    @lsheng : Yes, so use `currentWindow: true` to restrict it (or iterate through them) – Brett Zamir Jul 23 '18 at 23:44
  • 2
    So why doesn't `chrome.tabs.getCurrent` work in OP's code? It's my problem as well. – Shayan Dec 03 '19 at 12:14
  • 3
    Still relevant... 7 years later I don't know why they don't update their documentation on it – Eric Majerus May 27 '20 at 13:41
  • Please be aware that `chrome.tabs.query()` will run into race conditions when called in short order from multiple content scripts. Greg Wang's answer hints at a better way to do this. – Piers Uso Walter Nov 04 '21 at 22:00
  • 1
    When you need the tab object of a content page, send a dummy message to the background thread. The background thread receives the tab as a parameter and can include this in its response to the dummy message. Not only does this prevent race conditions, it's also faster because it does not use an asynchronous operation (unlike the call to `chrome.tabs.query()`) – Piers Uso Walter Nov 04 '21 at 22:08
22

Try:

chrome.tabs.getSelected(null, function(tab){
    console.log(tab);
});
serg
  • 109,619
  • 77
  • 317
  • 330
  • 13
    Deprecated, see @konstantin 's answer below. – lionello Feb 01 '14 at 07:02
  • `getCurrent()` is not nearly the same as the deprecated `getSelected()` or the answer below, the first method gets the tab from where the script is running within, while the second one gets the current selected(active) tab – novalain Aug 22 '16 at 09:37
6

Since chrome.tabs is only available in background or popup script and background script is not active in any tab, chrome.tabs.getCurrent() always return undefined.

Instead, we can retrieve the active Tab object from the second argument of any message listener callback. For example,

browser.runtime.onMessage.addListener((message, sender) => {
  console.log('Active Tab ID: ', sender.tab.id);
});
Greg Wang
  • 1,357
  • 1
  • 13
  • 17
0

May be undefined if called from a non-tab context (for example, a background page or popup view).

It looks like you should use this code not in bg.js but rather in cs.js.

B--rian
  • 5,578
  • 10
  • 38
  • 89
Jk L.
  • 21
  • 1
  • Welcome to SO, we appreciate your input. Please be so kind and be a bit more specific, I am not sure what the quotes are for - please mention who are you quoting Also: What is *this* code. Please edit your question. – B--rian Aug 01 '19 at 22:17
  • it is from the google official docs for chrome.tabs. https://developer.chrome.com/extensions/tabs#method-getCurrent. Unfortunately, I have tried it in cs.js, still did not work. it is a bug lol – Jk L. Aug 02 '19 at 06:56
  • It is more like you can call it from a page which has a browser tab. eg: options page. – Santosh May 31 '20 at 19:41
0

If you encounter this issue, problem can be caused by using chrome.tabs.getCurrent() in wrong place. You can't use chrome.tabs in content scripts. But you can use it in background or options scripts.

This example requires Manifest due to the use of Promises. Additionally, content scripts cannot use tabs.query.

Predrag Davidovic
  • 1,411
  • 1
  • 17
  • 20