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

前端 未结 4 904
甜味超标
甜味超标 2020-12-29 01:53

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

4条回答
  •  猫巷女王i
    2020-12-29 02:54

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

提交回复
热议问题