最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How determine if the popup page is open or not? - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 22

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.

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") {
发布评论

评论列表(0)

  1. 暂无评论