简体   繁体   中英

Is there a way to close a chrome extension popup, when the user is on a separate chrome window?

The scenario that I'm trying to get to work is:

If the user opens the extension popup on Chrome Window 1, and does not close the popup, the instant that the user opens another Chrome Window (Window 2) and starts working with it, I want the popup on Window 1 to close.

Basically, only the active Chrome Window can have the extension popup open.

Hm. I didn't know the popups worked this way!

But yes, it's possible. You can use the chrome.extension.getViews() function to access all popups and close others.

So, at the beginning of your popup code, execute this:

chrome.extension.getViews({type: "popup"}).forEach(function(win) {
  if(win != window) win.close(); 
});

It's quite an ancient API (you can tell because it's synchronous) but still works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM