Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Tab Extensions: getCurrent vs. getSelected?

I'm writing a Chrome extension. As part of the extension, I want to get the URL of the tab that the extension was called from. What's the difference between using:

chrome.tabs.getSelected(null, function(tab) { var myTabUrl = tab.url; });

and

chrome.tabs.getCurrent(function(tab) { var myTabUrl = tab.url; });

?

like image 352
grautur Avatar asked Jan 06 '11 19:01

grautur


People also ask

How can I get current tab ID?

If you send a message from the script, you will have access to a 'sender' object. From there, the tab ID is accessible via 'sender.tab.id'. See http://code.google.com/chrome/extensions/messaging.html for more info. You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.

How do I make Chrome tabs always active?

Open the Chrome menu (click the 3-dot menu in the upper-right corner of Chrome) Click Settings. Scroll to the On Startup section at the bottom of the page. Click to enable the setting Continue where you left off.


2 Answers

Method chrome.tabs.getSelected has been deprecated. You should use chrome.tabs.query instead now.

You can't find the official doc for obsolete method chrome.tabs.getSelected. Here is the doc for method chrome.tabs.query.

like image 197
weilou Avatar answered Oct 05 '22 16:10

weilou


getCurrent should be what you need, getSelected is a tab that is currently selected in a browser. When they could be different - maybe your extension runs some background cronjob in tabs, so that tab could be not currently selected by a user.

Ok I got it all wrong apparently. getCurrent should be used only inside extension's own pages that have a tab associated with them (options.html for example), you can't use it from a background or popup page. getSelected is a tab that is currently selected in a browser.

As to your original question - you probably need neither of those two. If you are sending a request from a content script to a background page, then the tab this request is being made from is passed as a sender parameter.

like image 29
serg Avatar answered Oct 05 '22 15:10

serg