User:Xytram/watchlistnotifier.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

/* Watchlist notifier ([[User:Ais523/watchlistnotifier.js]]); displays a message every time a watched page changes. */
//<pre><nowiki>
 
var wmwpajax;
// From [[WP:US]] mainpage (wpajax renamed to wmwpajax)
wmwpajax={
        download:function(bundle) {
                // mandatory: bundle.url
                // optional:  bundle.onSuccess (xmlhttprequest, bundle)
                // optional:  bundle.onFailure (xmlhttprequest, bundle)
                // optional:  bundle.otherStuff OK too, passed to onSuccess and onFailure
 
                var x = window.XMLHttpRequest ? new XMLHttpRequest()
                : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP")
                : false;
 
                if (x) {
                        x.onreadystatechange=function() {
                                x.readyState==4 && wmwpajax.downloadComplete(x,bundle);
                        };
                        x.open("GET",bundle.url,true);
                        x.send(null); 
                }
                return x;
        },
 
        downloadComplete:function(x,bundle) {
                x.status==200 && ( bundle.onSuccess && bundle.onSuccess(x,bundle) || true )
                || ( bundle.onFailure && bundle.onFailure(x,bundle) || alert(x.statusText+': '+bundle.url));
        }
};
 
// Example:
// function dlComplete(xmlreq, data) {
//      alert(data.message + xmlreq.responseText);
// }
//  wmwpajax.download({url:'http://en.wikipedia.org/w/index.php?title=Thresher&action=raw', 
//                   onSuccess: dlComplete, message: "Here's what we got:\n\n" });
 
// End of [[WP:US]] quote
 
function wmTimeDiff(i) {
var aMonth = new Array("NaN","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var objDate = new Date;
 
// split input into date/time values
var iDate = i.split("T")[0];
var iTime = i.split("T")[1].split("Z")[0];
// split iDate into y/m/d values and convert month to text
var iYear  = parseInt(iDate.split("-")[0]);
var iMonth = aMonth[parseInt(iDate.split("-")[1])];
var iDay   = iDate.split("-")[2];
///document.getElementById("contentSub").innerHTML += iMonth+", "+iDay+" "+iYear+" "+iTime;
 
// get i timestamp using parsed values
var iCTime = Date.parse(iMonth+", "+iDay+" "+iYear+" "+iTime);
///document.getElementById("contentSub").innerHTML += ". iCTime: "+iCTime
 
// get current timestamp and offset with timezone
var cCTime = objDate.getTime();
var cOffse = objDate.getTimezoneOffset();
var cOTime = cCTime + (cOffse * 60000);
///document.getElementById("contentSub").innerHTML += " ~ "+cCTime+" ("+cOffse+") = "+cOTime;
 
// convert to seconds and calculate time difference
cOTime = (cOTime/1000);
iCTime = (iCTime/1000);
var timeDiff = (iCTime<cOTime ? Math.floor(cOTime-iCTime) : 1);
///document.getElementById("contentSub").innerHTML += " ~ "+timeDiff+"secs";
 
// hours
timeHr = (timeDiff>3600 ? Math.floor(timeDiff/3600) : 0);
timeDiff = timeDiff - (timeHr*3600);
timeDisp = (timeHr>0 ? timeHr+"h" : "");
// minutes & seconds
timeMin = (timeDiff>60 ? Math.floor(timeDiff/60) : 0);
timeSec = Math.floor(((timeDiff/60) - timeMin) * 60);
timeDisp += timeMin+'m'+timeSec+'s';
///document.getElementById("time").innerHTML += timeDisp;
 
///document.getElementById('contentSub').innerHTML+= "<div>i:"+i+", "+cOTime+" - "+iCTime+" = "+timeDiff+"</div>";
return timeDisp;
}
 
 
function wmWatchEditFound(xmlreq, data) {
  var watchrev, watchsum, watchrevold, watchpage, junk;
  if(xmlreq.responseText.indexOf('revid=')==-1)
  {
    document.getElementById('contentSub').innerHTML+=
      "<div class='watchlistnotify'>(<i>watchlistnotifier can't determine whether a "+
      "watched page has changed<i>)</div>";
    return;
  }
  watchrev=xmlreq.responseText.split('revid="')[1].split('"')[0];
  try
  {
    watchrevold=document.cookie.split('ais523wmwatchrev=')[1].split('.')[0];
  }
  catch(junk) {watchrevold=0;}
  if(wgPageName == "Special:Watchlist")
  {
    document.cookie="ais523wmwatchrev="+watchrev+".; path=/";
    var aas=document.getElementById('bodyContent').getElementsByTagName('a');
    var i=aas.length;
    while(i--)
    {
      if(aas[i].href.indexOf('diff=')!=-1&&watchrevold)
        if(+(aas[i].href.split('diff=')[1].split('&')[0])>watchrevold)
          aas[i].parentNode.style.fontWeight='bold';
    }
  }
  else
  {
    //timestamp   
    watchest=xmlreq.responseText.split('timestamp="')[1].split('"')[0];
    watchsec=wmTimeDiff(watchest);
    //user
    watchuser=xmlreq.responseText.split('user="')[1].split('"')[0];
    watchuser=watchuser.split('<').join('&lt;').split('>').join('&gt;');
    //title
    watchpage=xmlreq.responseText.split('title="')[1].split('"')[0];
    watchpage=watchpage.split('<').join('&lt;').split('>').join('&gt;');
    //comment
    if (xmlreq.responseText.search(/comment/i) > 0) {
     watchsum=xmlreq.responseText.split('comment="')[1].split('"')[0];
     watchsum=watchsum.split('<').join('&lt;').split('>').join('&gt;');
    } 
    else { watchsum="-no edit summary-"; }
    watchsumt = watchsum;
    watchsum = (watchsum.length>50 ? watchsum.substr(0,50)+"..." : watchsum);
    // minor/bot
    var watchflag = "";
    if (xmlreq.responseText.search(/minor/i) > 0) { watchflag += "m"; }
    if (xmlreq.responseText.search(/bot/i) > 0) { watchflag += "b"; }
 
    //create div
    var div = "";
    if (watchflag != "") { div = '[<b>'+watchflag+'</b>] '; }
    div += '<a href="/wiki/User:'+watchuser+'">'+watchuser+'</a> changed <a href="/wiki/'+watchpage+'">'+watchpage+'</a> "<span style="font-variant:italic" title="'+watchsumt+'">'+watchsum+'</span>" about '+watchsec+' ago.';
    //display div
    if(watchrev!=watchrevold)
      document.getElementById('contentSub').innerHTML += div+' (<a href=\"/wiki/Special:Watchlist\">watchlist</a>)</div>';
  }
}
 
addOnloadHook(function() {
  /* Find the top item in the watchlist, and its edit summary. We only need one item, so
     set the limit to 1 to ease the load on the server. */
    wmwpajax.download({url:'http://en.wikipedia.org/w/api.php?action=query&list=watchlist&wllimit=1&'+
      'wldir=older&format=xml&wlprop=flags|ids|user|title|comment|timestamp', onSuccess: wmWatchEditFound});
});
// </nowiki></pre>
// [[Category:Wikipedia scripts]]