User:Yair rand/watchlistnotifications.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// Notifies the user when something new shows up on their watchlist. Desktop notifications only for Chrome and (I think) Firefox 5.
function watchlistNotifier(){
  var t, nc = window.webkitNotifications || window.mozNotifications || window.oNotifications || window.msNotifications || window.notifications || null;
  function checkWatchlistForUpdates(){
    JsMwApi()({action:'query',list:'watchlist',wlexcludeuser:wgUserName,wlprop:'user|title|timestamp',wlend:t?t:1,wlshow:mw.user.options.get("watchlisthidebots")=="1"?"!bot":""},function(r){
      r = r.query.watchlist;
      for(var i = 0; t && t < r[i].timestamp; i++){
        if(nc && nc.checkPermission() == 0){
          var w=nc.createNotification('',r[i].title,'has been edited by ' + r[i].user)
          w.show()
          w.onclick = function(){w.cancel();window.focus();}
          }
        document.title = r[i].title+' has been edited by ' + r[i].user
        onfocus = function(){location.reload(true)}
        }
      t = r[0].timestamp;
      setTimeout(checkWatchlistForUpdates, 15000)
      })
    }
  checkWatchlistForUpdates()
  if(nc && nc.checkPermission() == 1){
    document.getElementById('firstHeading').appendChild(newNode('a',"Enable watchlist desktop notifications",{'style':'float:right; cursor:pointer; font-size: 13px;',click:function(){nc.requestPermission(function(){location.reload()}); this.innerHTML = 'Waiting for permission to use desktop notifications...'}}))
    }
  }

wgPageName == "Special:Watchlist" && jQuery(document).ready(watchlistNotifier)