User:Darkwind/sandbox.js

/* Darkwind's Javascript Range Block Calculator * License: GFDL 1.3 or later*, CC-BY-SA-3.0*, or EPL 1.0 (your choice) * * Credit for IPv6 RegEx to David M. Syzdek (http://stackoverflow.com/a/17871737) * Credit for IPv4 RegEx to Chris Warwick (https://chrisjwarwick.wordpress.com/2012/09/16/more-regular-expressions-regex-for-ip-v4-addresses/) * */

var JRBC = {}; JRBC.regex = {};

JRBC.onLoad = function { var pageTitle = $("#firstHeading")[0].innerText; if (!pageTitle.includes("Revision history")) return; //do not load if the page is not a history page

$("#contentSub").append(' | Range block calculator'); //add execution link

//create regexen JRBC.regex.ipv4 = /\b((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b/g; JRBC.regex.ipv6 = /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/g; };

$(document).ready(JRBC.onLoad); //trigger the initial script processing when the page is done loading

JRBC.run = function { var rawIPv4 = [], rawIPv6 = [], sortedIPv4 = [], sortedIPv6 = []; var match, i;

if (JRBC.regex.ipv4.test($("#pagehistory")[0].innerHTML)) { JRBC.regex.ipv4.lastIndex = 0; //reset the regex

var matchesIPv4 = {}; while (match = JRBC.regex.ipv4.exec($("#pagehistory")[0].innerHTML)) { matchesIPv4[match[0]]=0; //assign each match as an object key, which automatically eliminates duplicates };

rawIPv4 = Object.keys(matchesIPv4); };

match = null; //for correct while testing

if (JRBC.regex.ipv6.test($("#pagehistory")[0].innerHTML)) { JRBC.regex.ipv6.lastIndex = 0; //reset the regex

var matchesIPv6 = {}; while (match = JRBC.regex.ipv6.exec($("#pagehistory")[0].innerHTML)) { matchesIPv6[match[0].toLowerCase]=0; //assign each match as an object key, which automatically eliminates duplicates };

rawIPv6 = Object.keys(matchesIPv6); };

if (rawIPv4.length > 0) { rawIPv4.sort(ipv4sort); }

debugger; };

function ipv4sort(a, b) { return dot2num(a) - dot2num(b); };

function dot2num(dot) { var d = dot.split('.'); return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]); };