开发者

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开发者_如何学Go) { var myTabUrl = tab.url; });

and

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

?


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.


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.


For those who is looking for working example of chrome.tabs.query instead of deprecated chrome.tabs.getSelected:

chrome.tabs.query({
    active: true,
    lastFocusedWindow: true
}, function (tabs) {
    var myTabUrl = tabs[0].url;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜