How can I get the current tab URL for chrome extension?

Google ChromeGoogle Chrome-Extension

Google Chrome Problem Overview


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>

Google Chrome Solutions


Solution 1 - Google Chrome

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

  });

Solution 2 - Google Chrome

The problem is in this line:

tab = tab.id;

It should be something like:

var tabId = tab.id;

Solution 3 - Google Chrome

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);
});

Solution 4 - Google Chrome

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);
});

Solution 5 - Google Chrome

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

chrome.tabs.query({
  active: true,
  currentWindow: true
}, ([currentTab]) => {
  console.log(currentTab.id);
});

Solution 6 - Google Chrome

Remember to add this to your manifest.json file

"permissions": [
    "tabs"
] 

Solution 7 - Google Chrome

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 });

Solution 8 - Google Chrome

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionryboView Question on Stackoverflow
Solution 1 - Google ChromeJonathan DumaineView Answer on Stackoverflow
Solution 2 - Google ChromesergView Answer on Stackoverflow
Solution 3 - Google ChromeKartik KkartikView Answer on Stackoverflow
Solution 4 - Google Chromejayant singhView Answer on Stackoverflow
Solution 5 - Google ChromerogyvojeView Answer on Stackoverflow
Solution 6 - Google ChromeAbbas KhanView Answer on Stackoverflow
Solution 7 - Google ChromeMuhammad OviView Answer on Stackoverflow
Solution 8 - Google ChromeComputer's GuyView Answer on Stackoverflow