3

I'm new in developing Chrome Extensions and a beginner in JavaScript. I need help in my chrome extension project.

Either if/else or a switch statement would be okay. I can't find a solution so far. I need my popup to show popup_1.html if the condition is true, and if it is false, then use popup_2.html

TylerH
  • 20,799
  • 66
  • 75
  • 101
Benz Tagle
  • 33
  • 3
  • Do you have any code to post? There are different ways to do this, as you can see from the answers. And without seeing your code it's difficult to know what would be the best solution. – Yogi Feb 14 '23 at 16:55
  • openpopup works only in dev version of chrome for now – Vitalik Jimbei Apr 03 '23 at 13:54

2 Answers2

1

use the chrome.browserAction.onClicked.addListener() method to listen for a click event on your extension's icon, and then use the chrome.tabs.create() method to create a new tab and load the HTML file

chrome.browserAction.onClicked.addListener(function() {
   
  var condition = true;

  if (condition) {
    chrome.tabs.create({ url: "popup_1.html" });
  } else {
    chrome.tabs.create({ url: "popup_2.html" });
  }
});

Rory
  • 437
  • 1
  • 13
  • Up vote, but it's important to note that the onClicked event will only fire when (1) the manifest `action.default_popup` is unset or (2) the default popup is cleared using `chrome.action.setPopup({ popup: "" });` – Yogi Feb 14 '23 at 16:48
1

First, Set html file by setPopup method
then open popup by openPopup(); method

const condition = true;
chrome.action.setPopup({ popup:condition ?  "popup_1.html": "popup_2.html"  });
chrome.action.openPopup();
Anil kumar
  • 1,216
  • 4
  • 12
  • upvote as using the default popup handler would be the better way. Yet, this solution depends on when and how the switch condition is set (details not provided in the question). – Yogi Feb 14 '23 at 16:52
  • Thanks! This solved my problem. But I remove the chrome.action.openPopup(); I removed it because it gives me an error in my chrome extension: "Uncaught TypeError: chrome.action.openPopup is not a function". The JS code still works even if I removed the chrome.action.openPopup(); what is it for? – Benz Tagle Feb 14 '23 at 17:17