简体   繁体   English

当用户在单独的镀铬窗口上时,有没有办法关闭chrome扩展弹出窗口?

[英]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. 如果用户在Chrome窗口1上打开了扩展程序弹出窗口,并且未关闭弹出窗口,则用户打开另一个Chrome窗口(窗口2)并开始使用它的那一刻,我希望窗口1上的弹出窗口关闭。

Basically, only the active Chrome Window can have the extension popup open. 基本上,只有活动的Chrome窗口才能打开扩展程序弹出窗口。

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. 您可以使用chrome.extension.getViews()函数来访问所有弹出窗口并关闭其他弹出窗口。

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. 这是一个相当古老的API(你可以告诉它,因为它是同步的),但仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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