User:Martyx/watchlistnotifier.js
/* Watchlist notifier (User:Ais523/watchlistnotifier.js); displays a message every time a watched page changes. */
//
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],10)];
var iDay = iDate.split("-")[2];
///document.getElementById("contentSub").innerHTML += "iDt:"+iDate+", iMo:"+iDate.split('-')[1];
///document.getElementById("contentSub").innerHTML += "aMo:"+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
///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+= "
i:"+i+", "+cOTime+" - "+iCTime+" = "+timeDiff+"";return timeDisp;
}
function wmWatchEditFound(xmlreq, data) {
var watchrev, watchsum, watchrevold, watchpage, junk;
if(xmlreq.responseText.indexOf('revid=')==-1)
{
document.getElementById('contentSub').innerHTML+=
"
(watchlistnotifier can't determine whether a "+";"watched page has changed)
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('<').split('>').join('>');
//title
watchpage=xmlreq.responseText.split('title="')[1].split('"')[0];
watchpage=watchpage.split('<').join('<').split('>').join('>');
//comment
if (xmlreq.responseText.search(/comment/i) > 0) {
watchsum=xmlreq.responseText.split('comment="')[1].split('"')[0];
watchsum=watchsum.split('<').join('<').split('>').join('>');
}
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 = '['+watchflag+'] '; }
div += ''+watchuser+' changed '+watchpage+' "'+watchsum+'" about '+watchsec+' ago.';
//display div
if(watchrev!=watchrevold)
document.getElementById('contentSub').innerHTML += div+' (watchlist)
}
}
$(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});
});
//