User:Tra/userwatchlist.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.
/////////////////////////////////////// // User watchlist // // by Tra // /////////////////////////////////////// function uwdownload(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 && uwdownloadComplete(x,bundle); }; x.open("GET",bundle.url,true); x.send(null); } return x; } function uwdownloadComplete(x,bundle) { x.status==200 && ( bundle.onSuccess && bundle.onSuccess(x,bundle) || true ) || ( bundle.onFailure && bundle.onFailure(x,bundle) || alert(x.statusText)); } function uwgetstartdate(uwdate) { var tempvar = '' tempvar = uwdate.getYear() tempvar += '' if ((uwdate.getMonth() + 1) < 10 ) { tempvar += '0' } tempvar += (uwdate.getMonth() + 1) tempvar += '' if (uwdate.getDate() < 10 ) { tempvar += '0' } tempvar += uwdate.getDate() tempvar += '000000' return tempvar } window.gettingWatchlist=false; uwwatchlist=null; function uwgetWatchlist() { window.gettingWatchlist=true; uwdownload({url: 'http://' + document.location.hostname + '/wiki/Special:Watchlist/edit', onSuccess: uwprocessWatchlist, onFailure: function () { setTimeout(uwgetWatchlist, 15000); return true; }}); } function uwprocessWatchlist(req, bundle) { var watchlist={}; var lines=req.responseText.split('\n'); var h1=document.getElementsByTagName('h1')[0] var myinfo=document.createElement('ul'); h1.parentNode.appendChild(myinfo); var uwnspg = '' var uwpropline = '' var uwdonestart = 'no' var uwquerystring = 'what=usercontribs&titles=' for (var i=0; i<lines.length; ++i) { if (lines[i].indexOf('<li><input type="checkbox" name="id[]" value=') > -1) { uwpropline = (lines[i].replace(/.*title="(.*?)">.*/, '$1')); uwnspg=uwpropline.split(':'); // eliminate all but user or user talk pages if ((uwnspg[0] == 'User' || uwnspg[0] == 'User talk') && uwpropline.split('/')[0]==uwpropline) { if (uwdonestart == 'yes') {uwquerystring += '|'} uwdonestart = 'yes' uwquerystring += 'User:' + uwnspg[1] //myinfo.innerHTML += '<li><a href="http://' + document.location.hostname + '/w/index.php?title=Special:Contributions&target=' + uwnspg[1] + '">' + uwnspg[1] + '</a></li>'; } } } var uwnow = new Date() uwquerystring += '&uclimit=20&ucstart=' + uwgetstartdate(uwnow) + '&uccomments&format=json&dummy=' + parseInt(Math.random()*99999999) // myinfo.innerHTML += '<li>' + uwquerystring + '</li>' uwdownload({url: 'http://' + document.location.hostname + '/w/query.php?' + uwquerystring, onSuccess: uwprocessuserwatchlist}); } function uwmid(str, start, len) /*** IN: str - the string we are LEFTing start - our string's starting position (0 based!!) len - how many characters from start we want to get RETVAL: The substring from start to start+len ***/ { // Make sure start and len are within proper bounds if (start < 0 || len < 0) return ""; var iEnd, iLen = String(str).length; if (start + len > iLen) iEnd = iLen; else iEnd = start + len; return String(str).substring(start,iEnd); } function uwtimefromdate(uwdate) { return uwmid(uwdate, 11, 5) } function uwprocessuserwatchlist(req, bundle) { var h1=document.getElementsByTagName('h1')[0] var uwoutput=document.createElement('div'); h1.parentNode.appendChild(uwoutput); uwoutput.innerHTML = ''; // uwoutput.innerHTML += req.responseText var uwtempoutput = '' var uwdata = eval('(' + req.responseText + ')'); var uwusernum = 0; var uwcontribnum = 0; var uwobjcontrib var uwusername uwtempoutput += ''; // uwtempoutput += uwdata.pages.length; // uwtempoutput += uwdata.pages.items[0].title; for (prop in uwdata.pages) { if (uwdata.pages[prop].contributions.length > 0) { uwusername = uwdata.pages[prop].title.split(':')[1] uwtempoutput += '<span id="RCM' + uwusernum + '"><a href="javascript:toggleVisibility(\'RCI' + uwusernum + '\',\'RCM' uwtempoutput += uwusernum + '\',\'RCL' + uwusernum + '\')"><img src="/skins-1.5/common/images/Arr_r.png"' uwtempoutput += ' width="12" height="12" alt="+" /></a></span><span id="RCL' + uwusernum + '" style="display:none">' uwtempoutput += '<a href="javascript:toggleVisibility(\'RCI' + uwusernum + '\',\'RCM' + uwusernum uwtempoutput += '\',\'RCL' + uwusernum + '\')"><img src="/skins-1.5/common/images/Arr_d.png" width="12"' uwtempoutput += ' height="12" alt="-" /></a></span><tt> </tt>' uwtempoutput += '<a href="/wiki/User:' + uwusername + '" title="User:' + uwusername + '">' + uwusername uwtempoutput += '</a> (<a href="/wiki/User_talk:' + uwusername + '" title="User talk:' + uwusername uwtempoutput += '">Talk</a> | <a href="/w/index.php?title=Special:Contributions&target=' + uwusername uwtempoutput += '" title="Special:Contributions">contribs</a>)' + '<br />'; uwtempoutput += '<div id="RCI' + uwusernum + '" style="display:none">' for (uwcontribnum = 0; uwcontribnum < uwdata.pages[prop].contributions.length; ++uwcontribnum){ uwobjcontrib = uwdata.pages[prop].contributions[uwcontribnum] uwtempoutput += '<img src="/skins-1.5/common/images/Arr_.png" width="12" height="12" alt=" " />' uwtempoutput += '<tt> </tt>' uwtempoutput += '<tt><a href="/w/index.php?title=' + uwobjcontrib['*'] + '&oldid=' + uwobjcontrib.revid uwtempoutput += '" title="' + uwobjcontrib['*'] + '">' + uwtimefromdate(uwobjcontrib.timestamp) + '</a></tt> ' uwtempoutput +='(<a href="/w/index.php?title=' + uwobjcontrib['*'] uwtempoutput += '&action=history" title="' + uwobjcontrib['*'] + '">hist</a>) (<a href="/w/index.php?title=' uwtempoutput += uwobjcontrib['*'] + '&diff=prev&oldid=' + uwobjcontrib.revid + '" title="' + uwobjcontrib['*'] uwtempoutput += '">diff</a>) <a href="/wiki/' + uwobjcontrib['*'] + '" title="' + uwobjcontrib['*'] + '">' uwtempoutput += uwobjcontrib['*'] + '</a> <span class="comment">(' + uwobjcontrib.comment + ')</span>' + '<br />' } uwtempoutput += '</div>' ++uwusernum } } uwtempoutput += ''; uwloadingmsg.innerHTML = '' uwoutput.innerHTML = uwtempoutput; if (typeof(window.setupTooltips)!='undefined') { setupTooltips(uwoutput); } } // ************************************************** // Installation // ************************************************** function uwaddlilink(tabs, url, name, id, title, key){ var na = document.createElement('a'); na.href = url; na.appendChild(document.createTextNode(name)); var li = document.createElement('li'); if(id) li.id = id; li.appendChild(na); tabs.appendChild(li); if(id) { if(key && title) ta[id] = [key, title]; else if(key) ta[id] = [key, '']; else if(title) ta[id] = ['', title]; } // re-render the title and accesskeys from existing code in wikibits.js akeytt(); return li; } function uwaddToolboxLink(url, name, id){ var tb = document.getElementById('p-tb').getElementsByTagName('ul')[0]; uwaddlilink(tb, url, name, id); } var uwloadingmsg function uwloadapp() { //var h1=document.getElementsByTagName('h1')[0] // var uwoutput=document.createElement('div'); //h1.innerHTML = 'User watchlist'; uwaddToolboxLink('http://' + document.location.hostname + '/wiki/' + 'User:Tra/User watchlist', 'User watchlist', 'uw_link'); if (wgPageName == 'User:Tra/User_watchlist') { var h1=document.getElementsByTagName('h1')[0] uwloadingmsg=document.createElement('div'); h1.parentNode.appendChild(uwloadingmsg); uwloadingmsg.innerHTML = 'Loading...' uwgetWatchlist() } } addOnloadHook(uwloadapp)