Skip to content

How to determine if you are inside the popup or inside a tab (chrome extension)

Published:

During the development of a chrome extension you can execute the content of “popup.html” inside the popup, or open the content in a new tab. Sometimes you need to know where the user is, so you can handle different actions for different contexts. How to determine it?

This simple function returns true if you are inside the popup otherwise false:

 const isInPopup = function() {
     return (typeof chrome != undefined && chrome.extension) ?
         chrome.extension.getViews({ type: "popup" }).length > 0 : null;
 }

if(isInPopup()){
  //Open the popup content in a new tab
  chrome.tabs.create({url: chrome.extension.getURL('popup.html')});
} else {
  //If you are already in the tab, do something else 
  ...
}