User:Ais523/watchTFA.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

var tfawpajax;
// From [[WP:US]] mainpage (wpajax renamed to tfawpajax)
tfawpajax={
        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 && tfawpajax.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));
        }
};

// Example:


// function dlComplete(xmlreq, data) {
//      alert(data.message + xmlreq.responseText);
// }
//  tfawpajax.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 WatchTFA(xmlreq, data)
{
  var tfaname=xmlreq.responseText.match(/\[\[([^[]*)\|more\.\.\.\]\]/)[1];
  location.href='http://en.wikipedia.org/w/index.php?title='+encodeURI(tfaname)+'&action=watch';
}

function IdentifyTFA()
{
  var d=new Date();
  var mn=new Array('January', 'February', 'March', 'April', 'May', 'June',
                   'July', 'August', 'September', 'October', 'November', 'December');
  tfawpajax.download({url:'http://en.wikipedia.org/w/index.php?title='+
                          'Wikipedia:Today%27s_featured_article/'+mn[d.getMonth()]+
                          '_'+d.getDate()+'%2C_'+d.getYear()+'&action=raw',onSuccess:WatchTFA});
}

addOnloadHook(function(){
  addPortletLink('p-tb','javascript:IdentifyTFA()','Watch TFA','t-watchtfa',
    "Watch today's Featured Article",'',null);
});
//[[Category:Wikipedia scripts]]