如何将焦点放在创建桌面通知的 Chrome 标签上? [英] How to get focus to a Chrome tab which created desktop notification?

查看:31
本文介绍了如何将焦点放在创建桌面通知的 Chrome 标签上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现与 Gmail 现在相同的功能.当收到新电子邮件或新聊天时,会出现通知弹出窗口,如果您单击它,Gmail 选项卡就会聚焦.

I would like to implement same functionality as Gmail has nowadays. When new email arrives or new chat comes, notification popup appears and if you click it, the tab with Gmail gets focussed.

我有这个代码:

var n = window.webkitNotifications.createNotification('ico.gif', 'Title', 'Text');
n.onclick = function(x) { this.cancel(); };
n.show();

当我点击通知时,它就会消失.现在我需要向 onclick 函数添加一些代码以显示并聚焦创建此通知的页面.我知道这是可能的,因为 GMail 做得很好.但是我没有成功查看 Gmail 来源(它们被最小化和混淆).

When I click notification it makes it just disappear. Now I need to add some code to onclick function to bring up and focus page that created this notification. I know it is possible because GMail does it very well. But I didn't succeed in looking into Gmail sources (they are minimalized and obfuscated).

有人知道怎么做吗?

推荐答案

您可以将 window.focus() 放在 Google Chrome 中.单击时,它将聚焦到该窗口.

You can just place window.focus() in Google Chrome. It will focus to that window when clicked.

var n = window.webkitNotifications.createNotification('ico.gif', 'Title', 'Text');
n.onclick = function(x) { window.focus(); this.close(); };
n.show();

我在 Gmail 中打开了检查器,添加了上面的代码,移到另一个选项卡,然后运行它.通知出现了,点击后,我又回到了 Gmail.

I opened the inspector in Gmail, added the above code, moved to a different tab, and ran it. The notification appeared and once clicked, it brought me back to Gmail.

这篇关于如何将焦点放在创建桌面通知的 Chrome 标签上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆