User:Ais523/sandbox.js: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
catch needs a param
what could be a final version, if it works
Line 41: Line 41:


function wmWatchEditFound(xmlreq, data) {
function wmWatchEditFound(xmlreq, data) {
var watchrev, watchsum, watchrevold, junk;
var watchrev, watchsum, watchrevold, watchpage, junk;
watchrev=xmlreq.responseText.split('revid="')[1].split('"')[0];
watchrev=xmlreq.responseText.split('revid="')[1].split('"')[0];
if(wgPageName == "Special:Watchlist")
if(wgPageName == "Special:Watchlist")
Line 48: Line 48:
{
{
watchsum=xmlreq.responseText.split('comment="')[1].split('"')[0];
watchsum=xmlreq.responseText.split('comment="')[1].split('"')[0];
watchpage=xmlreq.responseText.split('title="')[1].split('"')[0];
try
try
{
{
Line 53: Line 54:
}
}
catch(junk) {watchrevold=0;}
catch(junk) {watchrevold=0;}
watchsum=watchsum.split('<').join('&lt;').split('>').join('&gt;');
document.getElementById('contentSub').innerHTML+="Rev "+watchrev+", Summary "+escape(watchsum)+", was "+watchrevold+".";
watchpage=watchpage.split('<').join('&lt;').split('>').join('&gt;');
if(watchrev!=watchrevold)
document.getElementById('contentSub').innerHTML+=
"<div class='newmessages'>"+watchpage+'changed: "'+watchsum+'".</div>';
}
}
}
}

Revision as of 17:59, 19 February 2007

/* Watchlist notifier; 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 wmWatchEditFound(xmlreq, data) {
  var watchrev, watchsum, watchrevold, watchpage, junk;
  watchrev=xmlreq.responseText.split('revid="')[1].split('"')[0];
  if(wgPageName == "Special:Watchlist")
    document.cookie="ais523wmwatchrev="+watchrev+".";
  else
  {
    watchsum=xmlreq.responseText.split('comment="')[1].split('"')[0];
    watchpage=xmlreq.responseText.split('title="')[1].split('"')[0];
    try
    {
      watchrevold=document.cookie.split('ais523wmwatchrev=')[1].split('.')[0];
    }
    catch(junk) {watchrevold=0;}
    watchsum=watchsum.split('<').join('&lt;').split('>').join('&gt;');
    watchpage=watchpage.split('<').join('&lt;').split('>').join('&gt;');
    if(watchrev!=watchrevold)
      document.getElementById('contentSub').innerHTML+=
        "<div class='newmessages'>"+watchpage+'changed: "'+watchsum+'".</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=comment', onSuccess: wmWatchEditFound});
});