7
chrome.tabs.getCurrent(function(tab){
  alert(tab.id);
});

Is tab an object? Why doesn't it have an id property?

How can I get the id of the current tab?

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
woody
  • 83
  • 1
  • 1
  • 4

1 Answers1

7

chrome.tabs.getCurrent(function(tab){ console.log(JSON.stringify(tab,null, 2)); }) gives me next

{
  "active": true,
  "audible": false,
  "favIconUrl": "chrome-extension://eggkanocgddhmamlbiijnphhppkpkmkl/img/favicon.png",
  "height": 853,
  "highlighted": true,
  "id": 5280,
  "incognito": false,
  "index": 0,
  "mutedInfo": {
    "muted": false
  },
  "pinned": false,
  "selected": true,
  "status": "complete",
  "title": "Tabs Outliner",
  "url": "chrome-extension://eggkanocgddhmamlbiijnphhppkpkmkl/activesessionview.html?type=main&focusNodeId=5220&altFocusNodeId=5046&scrollToViewWinId=5046",
  "width": 400,
  "windowId": 5279
}

List of all props you can find at https://developer.chrome.com/extensions/tabs#type-Tab --- id marked as optional

The ID of the tab. Tab IDs are unique within a browser session. Under some >circumstances a Tab may not be assigned an ID, for example when querying >foreign tabs using the sessions API, in which case a session ID may be present. >Tab ID can also be set to chrome.tabs.TAB_ID_NONE for apps and devtools >windows.

That's important if you try run code from console

Tab ID can also be set to chrome.tabs.TAB_ID_NONE for apps and devtools >windows.

Vasiliy vvscode Vanchuk
  • 7,007
  • 2
  • 21
  • 44
  • Consider replacing your screenshot with the text content of `JSON.stringify(tab, null, 2);` or something. – Patrick Roberts May 29 '16 at 08:10
  • Yeah, it seems to be the only one possible method. I use it like this:`chrome.tabs.query({currentWindow: true, active: true}, function(tabs){ chrome.tabs.update(tabs[0].id, { url: "link"}); } );` – user25 Aug 27 '16 at 07:20