Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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> 
like image 566
rybo Avatar asked May 25 '11 23:05

rybo


People also ask

How do I find a URL extension?

It's as simple as Right Click > getURL. Open up the Extension popup window and you will be greeted with the parameters in a nicely formatted table.

What is the URL for Chrome extensions?

You can do it on your PC by enabling chrome://flags/#extensions-on-chrome-urls and adding the necessary url, chrome://extensions/ , into "matches" in manifest.


1 Answers

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    }); 
like image 103
Jonathan Dumaine Avatar answered Oct 20 '22 16:10

Jonathan Dumaine