I'm working on a Chrome extension and I'm looking how to find out (from the background page) if the popup page is open or not. I looked into message passing but I'm not sure if that's gonna help me at this or if there's a simpler way.
Thanks!
I'm working on a Chrome extension and I'm looking how to find out (from the background page) if the popup page is open or not. I looked into message passing but I'm not sure if that's gonna help me at this or if there's a simpler way.
Thanks!
Share Improve this question edited Jan 19, 2012 at 4:25 Camilo asked Jan 19, 2012 at 4:17 CamiloCamilo 7,1845 gold badges45 silver badges66 bronze badges2 Answers
Reset to default 22You can use the following chrome API call from your background page fetch if the popup view is open:
var views = chrome.extension.getViews({ type: "popup" });
//views => [] //popup is closed
//views => [DOMWindow] //popup is open
If it returns an empty array then your popup is not open, if it returns an array with your popups DOMWindow object, then your popup is open.
If you have multiple popups in one plugin then you could check for the existence of some global variable in the returned DOMWindow to disambiguate.
Alternative method:
extension.getViews({ type: "popup" })
will not work on mobile, because the popup is not actually a popup.
As a workaround, you can set a variable (say, document.title) in the popup's document, and test its value in the background page.
For instance, in your popup's HTML code:
<title>My awesome extension</title>
And in background.js
:
if (document.title == "My awesome extension") {