79

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,051
  • 2
  • 9
  • 14

8 Answers8

149

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

  });
Jonathan Dumaine
  • 5,295
  • 3
  • 35
  • 50
34

The problem is in this line:

tab = tab.id;

It should be something like:

var tabId = tab.id;
serg
  • 106,723
  • 76
  • 306
  • 327
  • 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
10

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);
});
jkdev
  • 10,404
  • 15
  • 54
  • 77
Kartik Kkartik
  • 109
  • 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,153
  • 5
  • 35
  • 50
jayant singh
  • 879
  • 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
1

Remember to add this to your manifest.json file

"permissions": [
    "tabs"
] 

Abbas Khan
  • 11
  • 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
  • 993
  • 13
  • 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
  • 4,557
  • 7
  • 49
  • 72