User:Splarka/contribsrange.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/contribsrange. |
/* Special:Contributions CIDR lookup, version [0.0.7]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/contribsrange.js
Notes:
* Requires at least http://svn.wikimedia.org/viewvc/mediawiki?view=rev&revision=30578
** May later require http://svn.wikimedia.org/viewvc/mediawiki?view=rev&revision=31312
* This uses the API, which is assloads faster than most other CIDR.
* Currently uses a GET via script src to avoid ajax problems.
* Only currently works if submitted.
* Due to URI length restrictions at Wikimedia, it has to split long queries (123.123.123.0/24 will show in two results).
To do:
* Pagination via timestamp of last listed contribution. Should be possible?
** Sortable by address? (not useful until pagination is available).
* More like normal Special:Recentchanges (hist)(diff) blah blah blah (except talk page existence for red/blue link)?
* Option to refresh on page? (not really needed, but might be more handy than onload action only).
* Support &ucuserprefix for wildcard/prefix search strings, and all /16 and /24
*/
if(wgCanonicalSpecialPageName == 'Contributions') {
addOnloadHook(contribsCIDRinit);
var cidrlimit = 500;
}
function contribsCIDRinit() {
var frm = document.getElementsByTagName('form')[0];
var res = document.createElement('div');
res.setAttribute('id','results-from-CIDR');
var prog = document.createElement('img');
prog.setAttribute('id','cidr-progress');
prog.setAttribute('src',stylepath + '/common/images/spinner.gif');
res.appendChild(prog);
frm.parentNode.appendChild(res);
var pattern = /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(2[4-9]|3(0-2))/i ;
if(frm.target.value.search(pattern) == 0) {
res.style.border = '1px solid black';
res.style.padding = '.5em';
var namespace = (parseInt(frm.namespace[frm.namespace.selectedIndex].value) > -1) ? '&ucnamespace=' + frm.namespace[frm.namespace.selectedIndex].value : '';
var cidr = frm.target.value.match(pattern)[0];
var range = cidr.match(/[^\/]\d{1,2}$/i)[0];
var oct3 = cidr.match(/\d{1,3}\//i)[0].replace('/','')[0];
cidr = cidr.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\./)[0];
var num = Math.pow(2,32 - range);
var start = oct3 - oct3 % num;
if(cidr.length >= 12 && num == 256) {
//this hack should be replaced with a prefix search when 31312 is active.
contribsCIDRscript(cidr,0,127,namespace);
contribsCIDRscript(cidr,128,255,namespace);
} else {
contribsCIDRscript(cidr,start,start + num,namespace);
}
}
}
function contribsCIDRscript(cidr,cidrStart,cidrEnd,namespace) {
var url = wgScriptPath + '/api.php?action=query&format=json&callback=contribsCIDR&list=usercontribs' + namespace + '&uclimit=' + parseInt(cidrlimit) + '&ucuser=';
for(var i=cidrStart;i<=cidrEnd;i++) {
url += '' + cidr + i;
if(i != cidrEnd) url += '|'
}
var scriptElem = document.createElement('script');
scriptElem.setAttribute('src',url);
scriptElem.setAttribute('type','text/javascript');
document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
function contribsCIDR(obj) {
if(!obj['query'] || !obj['query']['usercontribs']) return
var prog = document.getElementById('cidr-progress');
if(prog) prog.parentNode.removeChild(prog)
cidr = obj['query']['usercontribs'];
var res = document.getElementById('results-from-CIDR');
res.appendChild(document.createElement('hr'));
if(cidr.length == 0) {
res.appendChild(document.createTextNode('No changes were found for this CIDR range.'));
res.appendChild(document.createElement('hr'));
return;
}
res.appendChild(document.createTextNode(cidr.length + ' matches in the CIDR range specified (chronologically): '));
if(cidr.length == parseInt(cidrlimit)) {
res.appendChild(document.createElement('br'));
res.appendChild(document.createTextNode('NOTE: There are more results than the ' + cidrlimit + ' shown. You may not be seeing ALL contributors in this CIDR range.'));
}
res.appendChild(document.createElement('hr'));
for(var i=0;i<cidr.length;i++) {
res.appendChild(document.createTextNode('\u2022 ' + cidr[i].timestamp.replace(/T[\d:]*Z/,' ')));
addlinkchild(res, wgScript + '?title=Special:Contributions/' + cidr[i].user, cidr[i].user);
res.appendChild(document.createTextNode(' edited ('));
addlinkchild(res, wgScript + '?title=-&curid=' + cidr[i].pageid + '&diff=' + cidr[i].revid , 'diff');
res.appendChild(document.createTextNode(') '));
addlinkchild(res, wgScript + '?title=-&curid=' + cidr[i].pageid, cidr[i].title);
if(cidr[i].comment) res.appendChild(document.createTextNode(' (' + cidr[i].comment + ')'));
res.appendChild(document.createElement('br'));
}
}
function addlinkchild(obj,href,text,id,classes) {
if(!obj || !href || !text) return false;
var a = document.createElement('a');
a.setAttribute('href',href);
a.appendChild(document.createTextNode(text));
if(id) a.setAttribute('id',id);
if(classes) a.setAttribute('class',classes);
obj.appendChild(a);
}