如何专注于创建桌面通知的Chrome标签? [英] How to get focus to a Chrome tab which created desktop notification?

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

问题描述

我想实现与Gmail现在相同的功能。当新电子邮件到达或新聊天来临时,会出现通知弹出窗口,如果您点击该按钮,则带有Gmail的选项卡将得到重点关注。



我有这样的代码:

  var n = window.webkitNotifications .createNotification('ico.gif','标题','文本'); 
n.onclick = function(x){this.cancel(); };
n.show();

当我点击通知时,它就会消失。现在我需要添加一些代码到onclick函数来调出并关注创建此通知的页面。我知道这是可能的,因为GMail做得非常好。但是我没有成功查看Gmail资源(它们被简化和混淆)。



任何人都知道如何做到这一点? 在Google Chrome中放置window.focus()。点击后,它将关注到该窗口。

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

我在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.

I have this code:

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

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).

Anybody knows how to do this ?

解决方案

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.cancel(); };
n.show();

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.

Hope that helped!

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

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