How determine if the popup page is open or not?

时光毁灭记忆、已成空白 提交于 2020-02-03 04:58:48

问题


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!


回答1:


You 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.




回答2:


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") {


来源:https://stackoverflow.com/questions/8920953/how-determine-if-the-popup-page-is-open-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!