User:Future Perfect at Sunrise/rangeblock.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:Future Perfect at Sunrise/rangeblock. |
addOnloadHook(function() {
if (document.title.match(/^Block user - Wikipedia, the free encyclopedia$/)) {
//console.log("page found!");
var userBox = document.getElementsByName('wpTarget')[0];
if (userBox) {
//console.log("user box found!");
userBox.onchange = makeOptions;
makeOptions();
}
}
});
var patternIP = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
function isIP(txt) {
var found = patternIP.exec(txt);
if (found) {
for (var i = 0; i < 4; i++) {
if (found[i] > 255) return false;
}
return true;
} else return false;
}
function makeOptions(ip) {
var frm = getBlockForm();
if (frm) {
//console.log("Form found!");
var userBox = document.getElementsByName('wpTarget')[0];
if (userBox) {
var user = userBox.value;
var div = document.getElementById('rangeBlockSelectionDiv');
if (isIP(user)) {
var select = document.getElementById('rangeBlockSelector');
if (div) {
select.innerHTML = "";
}
else {
div = document.createElement('div');
div.id = "rangeBlockSelectionDiv";
div.appendChild(document.createTextNode("Range block? "));
select = document.createElement('select');
select.onchange = resetUser;
select.id = "rangeBlockSelector";
div.appendChild(select);
var parent = frm.parentNode;
var sibling = frm.nextSibling;
if (sibling) {
parent.insertBefore(div, sibling)
}
else {
parent.appendChild(div);
}
}
var minMask = 16;
var maxMask = 26;
var opt = document.createElement('option');
opt.value = user;
opt.innerHTML = "single IP (" + user + ")";
select.appendChild(opt);
for (var i = minMask; i < maxMask; i++) {
opt = makeOption(user, i);
select.appendChild(opt);
}
div.style.display = null;
}
else {
if (div) div.style.display = "none";
}
}
}
}
function makeOption(ip, mask) {
var parts = patternIP.exec(ip);
//console.log("Parts found: " + parts[1] + "-" + parts[2] + "-" + parts[3] + "-" + parts[4]);
var ipVal = (parts[1] * 0x1000000) |
(parts[2] * 0x10000) |
(parts[3] * 0x100) |
parts[4];
//console.log("IP value: " + ipVal.toString(16));
var maskVal = Math.pow(2, (32-mask)) - 1;
var rgVal = ipVal & maskVal;
if (rgVal < 0) rgVal += 0x100000000;
rgVal = (ipVal - rgVal);
//console.log("Range value: " + rgVal.toString(16));
parts = [0,0,0,0];
for (i = 3; i>=0; i--) {
parts[i] = rgVal % 0x100;
rgVal -= parts[i];
rgVal = rgVal >>> 8;
}
var cdir = parts[0] + "." +
parts[1] + "." +
parts[2] + "." +
parts[3] + "/" + mask;
var size = Math.pow(2, (32-mask));
var desc = cdir + " (" + size + " IPs )";
var opt = document.createElement('option');
opt.value = cdir;
//console.log("CDIR: " + cdir);
opt.innerHTML = desc;
return opt;
}
function getBlockForm() {
for (var i = document.forms.length - 1; i >= 0; i--) {
var frm = document.forms[i];
if (frm.action.search(/\bSpecial:Block\b/) >= 0) {
if (! frm.id) frm.id = "_myBlockForm";
document.forms._myBlockForm = frm;
return frm;
}
}
return null;
}
function resetUser() {
var val = this.value;
var userBox = document.getElementsByName('wpTarget')[0];
userBox.value = val;
var act = document.forms._myBlockForm.action;
act = act.replace(/Special:Block\/.+/, "Special:Block/" + val);
document.forms._myBlockForm.action = act;
}