我正在开发一款Chrome扩展程序,需要将桌面通知功能集成到其中。我的需求是,当用户单击通知窗口时,需要将其带到引起通知的标签页。我可以使用chrome.tabs API实现这一点,但我无法弄清楚如何在单击通知时将Chrome置于前台。
我知道chrome中禁用了window.focus(),但这绝对是可行的,因为Gmail桌面通知就是这种行为。
我正在开发一款Chrome扩展程序,需要将桌面通知功能集成到其中。我的需求是,当用户单击通知窗口时,需要将其带到引起通知的标签页。我可以使用chrome.tabs API实现这一点,但我无法弄清楚如何在单击通知时将Chrome置于前台。
我知道chrome中禁用了window.focus(),但这绝对是可行的,因为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();