88

I know there are many similar questions on SO, but I cannot seem to get it working.

I am trying to get the URL of the current tab from my Chrome extension. Hoewever, the alert(tab.url) returns "Undefined". I have added the "tabs" to my permissions in the manifest.json. Any ideas?

<html>
<head>
<script>

    chrome.tabs.getSelected(null, function(tab) {
        tab = tab.id;
        tabUrl = tab.url;

        alert(tab.url);
    });

</script>
</head>
rybo
  • 1,161
  • 2
  • 10
  • 14

9 Answers9

162

Just an FYI for people from Google:

The method OP uses is deprecated. To get the tab the user is viewing and only in the window they are viewing use this:

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
  // since only one tab should be active and in the current window at once
  // the return variable should only have one entry
  var activeTab = tabs[0];
  var activeTabId = activeTab.id; // or do whatever you need
});
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Jonathan Dumaine
  • 5,575
  • 3
  • 38
  • 50
34

The problem is in this line:

tab = tab.id;

It should be something like:

var tabId = tab.id;
serg
  • 109,619
  • 77
  • 317
  • 330
  • You changed the variable tab by mistake... careful next time :) – fedmich Feb 17 '12 at 23:32
  • 5
    getSelected is deprecated. Please use tabs.query {active: true} [tabs#method-getSelected](https://developer.chrome.com/extensions/tabs#method-getSelected) – contributorpw Apr 21 '14 at 16:32
  • Hi i need **focused tab info**.. if I have more than 5 tab in browser, if click (focused ) tab 1 then "action trigger" or tab 2/3/4/5.. every time a tab focused then it should trigger.so what can i do? – Mani Kasi Aug 28 '16 at 13:46
15

In manifest.json:

"permissions": [
    "tabs"
]

In JavaScript:

chrome.tabs.query({
    active: true,
    lastFocusedWindow: true
}, function(tabs) {
    // and use that tab to fill in out title and url
    var tab = tabs[0];
    console.log(tab.url);
    alert(tab.url);
});
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Kartik Kkartik
  • 159
  • 1
  • 2
9

This is what worked for me:

chrome.tabs.query({
    active: true,
    lastFocusedWindow: true
}, function(tabs) {
    // and use that tab to fill in out title and url
    var tab = tabs[0];
    console.log(tab.url);
    alert(tab.url);
});
Turnerj
  • 4,258
  • 5
  • 35
  • 52
jayant singh
  • 929
  • 12
  • 17
6

With a little help of ES6, you can easily write nicer code :)

chrome.tabs.query({
  active: true,
  currentWindow: true
}, ([currentTab]) => {
  console.log(currentTab.id);
});
rogyvoje
  • 304
  • 3
  • 6
4

The newest versions indicates it must be like this

async function getCurrentTab() {
  let queryOptions = { active: true, lastFocusedWindow: true };
  let [tab] = await chrome.tabs.query(queryOptions);
  return tab;
}

And you must add to your manifest.json this

"permissions: [
    "tabs"
]

Alvaro Fierro
  • 59
  • 1
  • 3
3

Remember to add this to your manifest.json file

"permissions": [
    "tabs"
] 

Abbas Khan
  • 31
  • 1
0

Just if someone using Web Extension Polyfill like me, for cross browser support.

// using async function
const [currentTab] = await browser.tabs.query({
  active: true,
  currentWindow: true,
});

console.log({ id: currentTab.id, url: currentTab.url });
Muhammad Ovi
  • 1,053
  • 14
  • 26
0

For me, I was missing the "tabs" permission as per Kartik Kkartin answer

Rest of the code in my background script

chrome?.tabs?.onUpdated?.addListener((tabId, changeInfo, tab) => {
  if (changeInfo.status == 'complete') {
    console.log('tabId', tabId, 'changeInfo', changeInfo, 'tab', tab)
  }
})

That will log the tab information, including the url in the tab.url when loading a tab

Computer's Guy
  • 5,122
  • 8
  • 54
  • 74