如何通过Web应用程序将chrome引入前景 [英] How to bring chrome to foreground from a web app
问题描述
是否有任何 windows应用程序/google chrome标记/google chrome扩展名/javascript方法
或任何其他允许将chrome窗口从背景(最小化)显示到前景的东西?当网站内部发生某些事情时.我确实可以访问客户端计算机,我可以设置chrome标志/信息亭模式或任何可能的阻止它打开到前台的东西.
Is there any windows apps/google chrome flags/google chrome extension/javascript methods
or any other thing that would allow to bring a chrome window from background(minimized) to foreground when something happens inside a website. I do have access to the client computer, I could set chrome flags/kiosk mode or anything that would prevent it from opening to foreground if it's possible.
例如,我试图通过通知将窗口置于前台(我正在使用Firebase消息).我试图在 service worker
的 setBackgroundMessageHandler()
中执行 window.open()
,但是在控制台中显示" window"时出现错误未定义
For example I am trying to bring the window to foreground when an order comes in, through a notification (I'm using firebase messages).
I tried to do window.open()
in setBackgroundMessageHandler()
in the service worker
but I get an error in console saying window is not defined
如果chrome无法实现此目标,那么是否创建具有适当权限的 Microsoft Progressive Web App 允许我将窗口从最小化状态变为前台?
If there isn't anything available for chrome to achieve this, would creating a Microsoft Progressive Web App with the appropriate permissions allow me to bring that window to foreground from a minimized state?
推荐答案
我通过将其添加到服务工作者文件中来解决它:
I solved it by adding this in the service worker file:
self.addEventListener('push', function (e) {
if (e.data) {
var payloadData = e.data.json();
self.clients.matchAll({includeUncontrolled: true}).then(function (clients) {
clients[0].postMessage(payloadData); // you can do foreach but I only needed one client to open one window
});
}
});
这在main.js文件中:
And this in the main.js file:
function openNotificationWindow(url) {
var myWindow = window.open(url); // Opens a new window
myWindow.focus();
myWindow.location.reload(true);
}
navigator.serviceWorker.addEventListener('message', function (event) {
if (typeof event.data.data !== 'undefined') {
openNotificationWindow(url);
}
});
现在,每次我在前台或后台窗口发送通知时,都会在serviceworker中触发 push
事件,然后使用 postMessage()
与一些main.js文件,您可以在其中访问 window
DOM组件.
Now everytime I send a notification either with the window in foreground or in the background the push
event fires in the serviceworker and then is using postMessage()
to comunicate with the some main.js file where you have access to the window
DOM component.
这篇关于如何通过Web应用程序将chrome引入前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!