简体   繁体   中英

Check if notification exists in JavaScript

Chrome browser.

The script is executed every time the page is refreshed.

Is it possible to verify the existence of a notification so as not to duplicate it.

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'
      });
    }
  })
}

Thanks mplungjan . So I thought to do it, but I thought there are still some solutions.

Working solution using 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);
            }
          })
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM