如何用JavaScript在加载视图中删除所有Chrome通知
How can remove all Chrome notifications in load view with JavaScript?
这是我在Google Chrome中显示通知的代码。
如何在代码中关闭通知?
document.addEventListener('DOMContentLoaded', function() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
});
function notifyMe() {
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('test', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "test",
});
notification.onclick = function() {
window.open("http://stackoverflow.com/a/13328397/1269037");
};
}
}
这很简单,每个通知对象都有close()
方法,你只需要将它们推送到一个数组中,并在窗口关闭前对每个通知对象调用close()
var notify=[];
for(var i=0; i<=4;i++){
var notification = new Notification('test', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "test"+i
}); //create some notifications
notify.push(notification);
}
function removeAllNotifys()
{
for(var i=0; i<notify.length;i++){
notify[i].close(); //remove them all
}
}
window.onbeforeunload = removeAllNotifys;
你也可以在一些按钮上关联removeAllNotifys()
点击清除所有通知或使用setTimeout来删除它们说2秒后。
相关文章:
- 地理定位API从Chrome 50中的不安全来源中删除
- Chrome内容脚本制作cookie;chrome重新启动时,cookie会被删除.如何使其稳定
- 如何动态添加或删除Chrome扩展、内容脚本、javascript文件
- javascript代码,用于删除firefox中的空白,但在IE和chrome中无法正常工作
- 如何从 Chrome 中的文件输入中删除“未选择文件”工具提示
- 在 chrome 中使用 document.removeChild() 删除脚本
- Chrome扩展程序 - 页面更新两次,然后在YouTube上删除
- 添加/删除类在 Chrome 中对我不起作用
- 当启用false或删除chrome扩展时,是否可以询问密码
- 使用正则表达式删除标记时,Firefox和Chrome之间存在不一致
- 当用户向下滚动我的网页时,如何删除chrome地址栏周围的边框
- chrome扩展删除脚本标记
- 删除属性后刷新DOM,而不象chrome那样刷新页面
- 在使用chrome.tabs.onUpdated.addListener后访问给定URL时删除侦听器
- String.trim 仅在 Chrome 39 和 Firefox 的两端都有时删除开头的空格
- Jquery.attr和.prop删除Chrome中的属性
- 在Windows上关闭NWJS应用程序后删除Chrome本地存储
- 删除chrome v8 javascript引擎函数缓存
- 如何删除chrome.storage.onChanged Event Listener
- 如何删除Chrome中以编程方式添加的样式