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

前端 未结 4 902
甜味超标
甜味超标 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条回答
  •  臣服心动
    2020-12-29 02:49

    Since chrome.tabs is only available in background or popup script and background script is not active in any tab, chrome.tabs.getCurrent() always return undefined.

    Instead, we can retrieve the active Tab object from the second argument of any message listener callback. For example,

    browser.runtime.onMessage.addListener((message, sender) => {
      console.log('Active Tab ID: ', sender.tab.id);
    });
    

提交回复
热议问题