如何判断页面是作为弹出窗口加载还是在 chrome 扩展中的单独选项卡中加载

How to tell if a page is loaded as a popup or in a separate tab in chrome extension

我的 Chrome 扩展有一个页面,可以在弹出窗口中看到,也可以作为单独的选项卡看到。当它被视为一个单独的选项卡时,我需要在页面的一角显示一个小按钮。但是我找不到检测页面何时加载到其自己的选项卡中的方法。

使用 chrome.extension.getViews,其中 returns 一个包含 window 个对象的数组。

var tabs = chrome.extension.getViews({ type: "tab"})
if(tabs[0]) {
  console.log("inside tab")
}
var popups = chrome.extension.getViews({ type: "popup"})
if(popups[0]) {
  console.log("inside popup")
}

或回调中chrome.tabs.getCurrent, which returns a tab object

chrome.tabs.getCurrent(function(tab) {
  if(tab) {
    console.log("inside tab")
  } else {
    console.log("inside popup")
  }
})