簡體   English   中英

檢查 JavaScript 中是否存在通知

[英]Check if notification exists in 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);
            }
          })
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM