User:Splarka/stalkcontribs.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Splarka/stalkcontribs. |
/* Ajax user contributions stalker, version [0.0.2]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/stalkcontribs.js
Does stuff.
*/
if(mw.config.get('wgCanonicalSpecialPageName') && mw.config.get('wgCanonicalSpecialPageName') == 'Contributions') addOnloadHook(stalkContribsInit)
//globals
var stalkingRefreshSeconds = '60';
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',mw.config.get('wgScript') + '?title=' + mw.config.get('wgPageName') + '&target=' + target + '&stalk=true','Stalk','ca-stalk','Stalk user for new contribs.');
}
}
function stalkContribs(target) {
mw.notify('Fetching timestamp of latest contribution for: ' + decodeURIComponent(target) + ' ...');
var url = mw.config.get('wgScriptPath') + '/api.php?action=query&format=json&list=usercontribs&uclimit=1&ucprop=timestamp&ucuser=' + target;
var req = new XMLHttpRequest();
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']) {
mw.notify('Api error: ' + obj['error']['code'] + ' - ' + obj['error']['info']);
return;
}
if(!obj['query'] || !obj['query']['usercontribs']) {
mw.notify('Error: Unexpected response: ' + txt);
return;
}
if(!obj['query']['usercontribs'][0] || !obj['query']['usercontribs'][0]['timestamp'] || !obj['query']['usercontribs'][0]['user']) {
// user has no contribs?
var ts = '0';
var target = queryString('target') || encodeURIComponent(ucfrm.target.value) || false;
if(!target) {
mw.notify('Error: no target found');
return;
}
} else {
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;
mw.notify(msg); document.title = msg; alert(msg);
//window.location.reload(); //a bit agressive, purges css/js too
window.location.replace(window.location.href);
} else {
mw.notify('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;
}