Jump to content

User:Future Perfect at Sunrise/rangeblock.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Future Perfect at Sunrise (talk | contribs) at 00:03, 6 February 2012 (new). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
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;
}