How do you use chrome.tabs.getCurrent to get the page object in a Chrome extension?

a 夏天 提交于 2019-11-30 06:15:25

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]);
});
Jk L.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!