User:Kanonkas/stalkcontribs.js

/* Ajax user contributions stalker, version [0.0.1] Originally from: http://en.wikipedia.org/wiki/User:Splarka/stalkcontribs.js Does stuff. if(wgCanonicalSpecialPageName && wgCanonicalSpecialPageName == 'Contributions') addOnloadHook(stalkContribsInit) //globals var stalkingRefreshSeconds = '10'; var stalkingTS = ''; function stalkContribsInit { var ucfrm = document.getElementsByTagName('form')[0]; var target = queryString('target') || encodeURIComponent(ucfrm.target.value) || false; if(!target || target == '') return if(queryString('stalk')) { stalkContribs(target); } else { mw.util.addPortletLink('p-cactions',wgScript + '?title=' + wgPageName + '&target=' + target + '&stalk=true','Stalk','ca-stalk','Stalk user for new contribs.'); } } function stalkContribs(target) { jsMsg('Fetching timestamp of latest contribution for: ' + decodeURIComponent(target) + ' ...'); var url = wgScriptPath + '/api.php?action=query&format=json&list=usercontribs&uclimit=1&ucprop=timestamp&ucuser=' + target; var req = sajax_init_object; req.open('GET', url, true); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { eval("stalkContribsCB(" + req.responseText + ",'" + req.responseText.replace(/\'/g,"`") + "')"); } }  req.send(null); } function stalkContribsCB(obj,txt) { if(obj['error']) { jsMsg('Api error: ' + obj['error']['code'] + ' - ' + obj['error']['info']); return; } if(!obj['query'] || !obj['query']['usercontribs'] || !obj['query']['usercontribs'][0] || !obj['query']['usercontribs'][0]['timestamp'] || !obj['query']['usercontribs'][0]['user']) { jsMsg('Error: Unexpected response: ' + txt); return; } var ts = obj['query']['usercontribs'][0]['timestamp']; var target = obj['query']['usercontribs'][0]['user']; if(stalkingTS == '') stalkingTS = ts if(stalkingTS != ts) { var msg = 'New contributions detected for ' + target; jsMsg(msg); document.title = msg; alert(msg); //window.location.reload; //a bit agressive, purges css/js too window.location.replace(window.location.href); } else { jsMsg('Timestamp of latest contribution for ' + decodeURIComponent(target) + ' is: ' + ts); setTimeout('stalkContribs("' + encodeURIComponent(target) + '")', stalkingRefreshSeconds * 1000); } } function queryString(p) { var re = RegExp('[&?]' + p + '=([^&]*)'); var matches; if (matches = re.exec(document.location)) { try { return decodeURI(matches[1]); } catch (e) { } }  return null; }