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.4]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/contribsrange.js
Notes:
* 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 with /24.
* 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?
* More like normal Special:Recentchanges (hist)(diff) blah blah blah (talk page existence for link wouldn't be worthwhile)?
* Option to have /23 /22 /21 etc. Require more flexible regex and smarter start/end handling.
* Option to refresh on page? (not really needed, but might be more handy than onload action only).
* Anything else?
*/
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');
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]?)\.0\/24/i ;
if(frm.target.value.search(pattern) == 0) {
res.style.border = '1px solid black';
res.style.padding = '.5em';
var cidr = frm.target.value.match(pattern)[0].replace(/0\/24/,'');
var namespace = (parseInt(frm.namespace[frm.namespace.selectedIndex].value) > -1) ? '&ucnamespace=' + frm.namespace[frm.namespace.selectedIndex].value : '';
if(cidr.length < 12) {
contribsCIDRscript(cidr,0,255,namespace);
} else {
//this would be a long-ass URL (over 4000 characters).
contribsCIDRscript(cidr,0,127,namespace);
contribsCIDRscript(cidr,128,255,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
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.'));
return;
}
res.appendChild(document.createTextNode(cidr.length + ' matches in the CIDR range specified: '));
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'));
res.appendChild(document.createTextNode('List of the contribs found:'));
res.appendChild(document.createElement('br'));
for(var i=0;i<cidr.length;i++) {
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);
}