Jump to content

User:Opencooper/IPtoEmoji.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Opencooper (talk | contribs) at 08:08, 10 July 2019 (tweak). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// Converts IP addresses to emoji for better recognizability

// Uses a lookup table of 256 arbritrary emoji and uses the 8-bit hex parts of the
// address to pick the emoji. For IPv6, 16-bit hextets are split into two bytes.

// Doesn't work in IE due to method used to pad strings, but if you're still
// using that, you probably don't even know what emoji are ;)

// Limitations: only works on interface pages, not manually linked IP addresses
// such as on AIV

// Interesting IPs: 2804:14D:5C59:8300:0:0:0:1000

// Stolen from: https://gist.github.com/windytan/7910910/
var emojiTable = [ "🌀", "🌂", "🌅", "🌈", "🌙", "🌞", "🌟", "🌠", "🌰", "🌱", "🌲", "🌳", "🌴", "🌵", "🌷", "🌸", "🌹", "🌺", "🌻", "🌼", "🌽", "🌾", "🌿", "🍀", "🍁", "🍂", "🍃", "🍄", "🍅", "🍆", "🍇", "🍈", "🍉", "🍊", "🍋", "🍌", "🍍", "🍎", "🍏", "🍐", "🍑", "🍒", "🍓", "🍔", "🍕", "🍖", "🍗", "🍘", "🍜", "🍝", "🍞", "🍟", "🍠", "🍡", "🍢", "🍣", "🍤", "🍥", "🍦", "🍧", "🍨", "🍩", "🍪", "🍫", "🍬", "🍭", "🍮", "🍯", "🍰", "🍱", "🍲", "🍳", "🍴", "🍵", "🍶", "🍷", "🍸", "🍹", "🍺", "🍻", "🍼", "🎀", "🎁", "🎂", "🎃", "🎄", "🎅", "🎈", "🎉", "🎊", "🎋", "🎌", "🎍", "🎎", "🎏", "🎒", "🎓", "🎠", "🎡", "🎢", "🎣", "🎤", "🎥", "🎦", "🎧", "🎨", "🎩", "🎪", "🎫", "🎬", "🎭", "🎮", "🎯", "🎰", "🎱", "🎲", "🎳", "🎴", "🎵", "🎷", "🎸", "🎹", "🎺", "🎻", "🎽", "🎾", "🎿", "🏀", "🏁", "🏂", "🏃", "🏄", "🏆", "🏇", "🏈", "🏉", "🏊", "🐀", "🐁", "🐂", "🐃", "🐄", "🐅", "🐆", "🐇", "🐈", "🐉", "🐊", "🐋", "🐌", "🐍", "🐎", "🐏", "🐐", "🐑", "🐒", "🐓", "🐔", "🐕", "🐖", "🐗", "🐘", "🐙", "🐚", "🐛", "🐜", "🐝", "🐞", "🐟", "🐠", "🐡", "🐢", "🐣", "🐤", "🐥", "🐦", "🐧", "🐨", "🐩", "🐪", "🐫", "🐬", "🐭", "🐮", "🐯", "🐰", "🐱", "🐲", "🐳", "🐴", "🐵", "🐶", "🐷", "🐸", "🐹", "🐺", "🐻", "🐼", "🐽", "🐾", "👀", "👂", "👃", "👄", "👅", "👆", "👇", "👈", "👉", "👊", "👋", "👌", "👍", "👎", "👏", "👐", "👑", "👒", "👓", "👔", "👕", "👖", "👗", "👘", "👙", "👚", "👛", "👜", "👝", "👞", "👟", "👠", "👡", "👢", "👣", "👤", "👥", "👦", "👧", "👨", "👩", "👪", "👮", "👯", "👺", "👻", "👼", "👽", "👾", "👿", "💀", "💁", "💂", "💃", "💄", "💅" ];
// Pretty rudimentary, but passable since we only use it in restricted circumstances
// Chokes on "User:[IP]" for IPv6
var IPregex = /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[0-9A-F]*:[0-9A-F]*:[0-9A-F]*:[0-9A-F]*:[0-9A-F]*:[0-9A-F]*:[0-9A-F]*:?[0-9A-F]*)/gi;

function addAnonLinkEmoji() {
	var IP = $(this).text();
	var emoji = convertIP(IP);
	$(this).append("<span class='IP-emoji'> [" + emoji + "]</span>");

}

function addContribEmoji() {
	var text = $(".mw-contributions-user-tools").text();
	var match = text.match(IPregex);
	if (match) {
		var IP = match[0];
		var emoji = convertIP(IP);
		$("#contentSub").append("<span class='IP-emoji'> [" + emoji + "]</span>");
	}
}

function addUserTalkEmoji() {
	var text = mw.config.get("wgTitle");
	var match = text.match(IPregex);
	if (match) {
		var IP = match[0];
		var emoji = convertIP(IP);
		$("h1").append("<span class='IP-emoji'> [" + emoji + "]</span>");
	}
}

function addHrefEmoji() {
	var text = $(this).text();
	var href = $(this).attr("href");
	var match = text.match(IPregex);
	if (match && IPregex.test(href)) {
		var IP = match[0];
		var emoji = convertIP(IP);
		$(this).append("<span class='IP-emoji'> [" + emoji + "]</span>");
	}
}

function convertIP(IP) {
	var base;
	var conversion = "";
	var groups = [];

	if (/\./.test(IP)) { // IPv4
		base = 10;
		groups = IP.split(".");
	} else if (/:/.test(IP)) { // IPv6
		base = 16;
		// Sometimes two consecutive zero-groups are omitted with "::"
		IP = IP.replace(/::/g, ":0:0:");

		v6groups = IP.split(":");
		// Since IPv6 has 2-byte groups, we need to split those bytes
		for (let i=0; i<v6groups.length; i++) {
			var v6group = v6groups[i];
			if (v6group.length < 4) {
				v6group = v6group.padStart(4, "0");
			}
			groups.push(v6group.slice(0, 2));
			groups.push(v6group.slice(2, 4));
		}
	} else {
		console.error("User:Opencooper/IPtoEmoji.js: " + IP + " was not readable as an IP address");
		return;
	}
	
	for (let i=0; i<groups.length; i++) {
		var index = parseInt(groups[i], base); 
		conversion += emojiTable[index];
	}

	return conversion;
}

// Generic interace links to IP addresses
$(".mw-anonuserlink").each(addAnonLinkEmoji);

// Contributions pages
if (/Special:Contributions/.test(window.location.pathname)) {
	addContribEmoji();
}

// User talk pages
if (mw.config.get('wgNamespaceNumber') == 3) {
	addUserTalkEmoji();
}

// Links to contrib pages
$("#mw-content-text a:not(.mw-userlink)").each(addHrefEmoji);