javascript - 检查 JavaScript 中是否存在通知
问题描述
铬浏览器。
每次刷新页面时都会执行该脚本。
是否可以验证通知的存在以免重复。
if ($('.snippet__title').length) {
var titles = document.querySelectorAll('.snippet__title')
titles.forEach((title) => {
if (title.textContent.includes('searchString')) {
var msg = title.textContent.trim()
var notification = new Notification('Element found', {
body: msg,
dir: 'auto',
icon: 'icon.jpg'
});
}
})
}
解决方案
谢谢mplungjan。所以我想这样做,但我认为仍然有一些解决方案。
使用 localStorage 的工作解决方案
var NotifyShowed = localStorage.getItem('NotifyShowed')
if (!NotifyShowed) {
if ($('.snippet__title').length) {
var titles = document.querySelectorAll('.snippet__title')
titles.forEach((title) => {
if (title.textContent.includes('searchString')) {
var msg = title.textContent.trim()
var notification = new Notification('Element found', {
body: msg,
dir: 'auto',
icon: 'icon.jpg'
});
localStorage.setItem('NotifyShowed', true);
}
})
}
}
推荐阅读
- python - pysimplegui 表格多行文本处理
- python - 使用 matplotlib 绘制数组的空白图
- javascript - Uncaught TypeError: Glide is not a constructor - 使用 Glide.js 和 Laravel
- atom-editor - 如何在 Atom 中使用 love2d 打印
- python - Kubernetes pods:如何获取 pod 统计信息?
- c# - 在不更改当前 URL 的情况下重定向到另一台服务器
- inappsettingskit - 如何使用 InAppSettingsKit 重用 plist 文件?
- pandas - 使用多个索引重塑 Pandas 数据框
- javascript - Angular + Leaflet + Leaflet AwesomeMarkers - “无法读取未定义的属性‘图标’”
- reactjs - 在 reactJS 中未获取 API 数据