The code is meant to output the current tab object for the page the user is viewing to the console but it just outputs undefined. It's run from within a browser action page.
chrome.tabs.getCurrent( function(tab){
console.log(tab);
} );
I've looked at the documentation and as far as I can tell the code seems to match what it says.
Try:
chrome.tabs.getSelected(null, function(tab){
console.log(tab);
});
The method getSelected()
has been deprecated since Google Chrome 16 (but many articles in the official documentation had not yet been updated). Official message is here. To get the tab that is selected in the specified window, use chrome.tabs.query()
with the argument {'active': true}
. So now it should look like this:
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
console.log(tabs[0]);
});
May be undefined if called from a non-tab context (for example, a background page or popup view).
It looks like you should use this code not in bg.js
but rather in cs.js
.
来源:https://stackoverflow.com/questions/6718256/how-do-you-use-chrome-tabs-getcurrent-to-get-the-page-object-in-a-chrome-extensi