Jump to content

User:Yair rand/watchlistnotifications.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Notifies the user when something new shows up on their watchlist, excluding one's own edits.
// Desktop notifications only work for Chrome at the moment.
// To enable, add importScript("User:Yair rand/watchlistnotifications.js"); to Special:MyPage/common.js, 
// go to your watchlist, and click the "Enable watchlist desktop notifications" button in the top-right corner.
// This is a modified version of the earlier [[wikt:User:Yair rand/watchlistnotifications.js]].
function watchlistNotifier(){
  var t, nc = window.webkitNotifications || window.mozNotifications || window.oNotifications || window.msNotifications || window.notifications || null;
  function checkWatchlistForUpdates(){
    $.get(mw.config.get('wgServer')+mw.config.get('wgScriptPath')+"/api.php?format=json&action=query&list=watchlist&wlexcludeuser="+mw.config.get('wgUserName')+"&wlprop=user|title|timestamp&wlend="+(t?t:1),function(r){
      r = r.query.watchlist;
      for(var i = 0; t && t < r[i].timestamp; i++){
        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)
      })
    }
  nc && nc.checkPermission() == 0 && checkWatchlistForUpdates()
  if(nc && nc.checkPermission() == 1){
    var a = document.getElementById('firstHeading').appendChild(document.createElement('a'));
    a.appendChild(document.createTextNode("Enable watchlist desktop notifications"));
    a.style.float = 'right'; a.style.cursor = 'pointer'; a.style.fontSize = '13px';
    a.onclick = function(){nc.requestPermission(function(){location.reload()}); this.innerHTML = 'Waiting for permission to use desktop notifications...'}
    }
  }

mw.config.get('wgPageName') == "Special:Watchlist" && jQuery(document).ready(watchlistNotifier)