点击 Chrome 桌面通知即可专注于内容

Chrome desktop notification click to focus on content

本文关键字:专注 于内容 通知 Chrome 桌面 点击      更新时间:2023-09-26

我正在将桌面通知构建到我正在处理的chrome扩展程序中。 我需要的功能要求用户在单击通知窗口时被带到导致通知的选项卡。 我可以使用chrome.tabs API来工作,但是我无法弄清楚的是,当单击通知时,如何将Chrome置于最前面。

我知道window.focus()在chrome中被禁用,但这绝对是可能的,因为这是Gmail桌面通知的行为。

notification = webkitNotifications.createNotification(...)
notification.onclick = function(){
    window.focus();
    this.cancel();
};
notification.show()

。按预期工作,无需任何其他权限。

使用 chrome.tabs.update(tabId, {active: true}); 聚焦选项卡(不要与 chrome.windows.update 混淆)。

tabId通常通过Tab类型获得。此对象传递给许多方法/事件侦听器(有时通过 MessageSender 类型)。

function msg(){
    var notification = new Notification("Title", {body: "Yore message", icon: "img.jpg" });
    notification.onshow = function() { setTimeout(notification.close(), 15000); };
    notification.onclick = function(){
        window.focus();
        this.cancel();
    };
}
msg();