如何将焦点转移到创建桌面通知的 Chrome 选项卡?

发布于 2024-10-16 04:00:17 字数 390 浏览 1 评论 0原文

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

我有这样的代码:

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

当我单击通知时,它就会消失。现在我需要向 onclick 函数添加一些代码,以显示并聚焦创建此通知的页面。我知道这是可能的,因为 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘你却要生生世世 2024-10-23 04:00:17

您只需将 window.focus() 放置在 Google Chrome 中即可。单击时它将聚焦到该窗口。

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

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

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();

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.

柠檬心 2024-10-23 04:00:17

使用通知

if (typeof Notification !== 'undefined') {
  alert('Please us a modern version of Chrome, Firefox, Opera or Safari.');
  return;
}

Notification.requestPermission(function (permission) {
  if (permission !== 'granted') return;

  var notification = new Notification('Here is the title', {
    icon: 'http://path.to/my/icon.png',
    body: 'Some body text',
  });

  notification.onclick = function () {
    window.focus();
  };
});

Using Notifications.

if (typeof Notification !== 'undefined') {
  alert('Please us a modern version of Chrome, Firefox, Opera or Safari.');
  return;
}

Notification.requestPermission(function (permission) {
  if (permission !== 'granted') return;

  var notification = new Notification('Here is the title', {
    icon: 'http://path.to/my/icon.png',
    body: 'Some body text',
  });

  notification.onclick = function () {
    window.focus();
  };
});
泛泛之交 2024-10-23 04:00:17

window.focus() 在最新的 Webkit 浏览器版本(Chrome、Safari 等)中并不总是有效。但 parent.focus() 确实如此。

这是一个完整的jsfiddle: https://jsfiddle.net/wv0w7uj7/3/

代码:

function notifyMe() {
  if (Notification.permission !== "granted")
    Notification.requestPermission();
  else {
    var notification = new Notification('Notification title', {
      icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
      body: "You've been notified!",
    });

    notification.onclick = function () {
      parent.focus();
      window.focus(); //just in case, older browsers
      this.close();
    };
  }
}

window.focus() does not always work in recent Webkit browser versions (Chrome, Safari etc). But parent.focus() does.

Here's a complete jsfiddle: https://jsfiddle.net/wv0w7uj7/3/

Code:

function notifyMe() {
  if (Notification.permission !== "granted")
    Notification.requestPermission();
  else {
    var notification = new Notification('Notification title', {
      icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
      body: "You've been notified!",
    });

    notification.onclick = function () {
      parent.focus();
      window.focus(); //just in case, older browsers
      this.close();
    };
  }
}
柒夜笙歌凉 2024-10-23 04:00:17

使用 onclick 属性并不是很好的做法,对于普通 javascript 使用 addEventListener 或对于 jQuery 使用 on 方法。

var notify = new Notification('Test notification');

普通:

notify.addEventListener('click', function(e) {
    window.focus();
    e.target.close();
}, false);

jQuery:

$(notify).on('click', function(e) {
    window.focus();
    e.target.close();
});

It's not really good practice to use the onclick property, use the addEventListener for vanilla javascript or on method for jQuery.

var notify = new Notification('Test notification');

Vanilla:

notify.addEventListener('click', function(e) {
    window.focus();
    e.target.close();
}, false);

jQuery:

$(notify).on('click', function(e) {
    window.focus();
    e.target.close();
});
情仇皆在手 2024-10-23 04:00:17

它应该是 this.close() 而不是 this.cancel(),如下所示:

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

It should be this.close() rather than this.cancel(), like this:

var n = window.webkitNotifications.createNotification('ico.gif','Title', 'Text');
n.onclick = function(x) { window.focus(); this.cancel(); };
n.show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文