User:Wugapodes/CapricornUtilsDev.js

Source: Wikipedia, the free encyclopedia.
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.
// <nowiki>
//
// Utility functions for Capricorn user script. Development version.
//
// This is a modified version of [[User:Sam Sailor/Scripts/Sagittarius+.js]] ([[Special:PermaLink/899463476]])
// Docs: [[User:Wugapodes/Capricorn]]

/*jshint undef:true, latedef:true, shadow:true, loopfunc:true, scripturl:true, undef:true */
/*globals jQuery, mw, importStylesheet */
////////////////////////////////////////////////////////////////////////////////
// Helper function definitions
function encodeCodePoint(c) {
	if (c === 0x20)
		return '_';
	if (c < 0x80) {
		return '.' + c.toString(16).toUpperCase();
	} else if (c < 0x800) {
		return '.' + (0xc0 |  (c >>>  6)        ).toString(16).toUpperCase() +
			'.' + (0x80 | ( c         & 0x3f)).toString(16).toUpperCase();
	} else if (c < 0x10000) {
		return '.' + (0xe0 |  (c >>> 12)        ).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>>  6) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ( c         & 0x3f)).toString(16).toUpperCase();
	} else if (c < 0x200000) {
		return '.' + (0xf0 |  (c >>> 18)        ).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>> 12) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>>  6) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ( c         & 0x3f)).toString(16).toUpperCase();
	} else if (c < 0x4000000) {
		return '.' + (0xf8 |  (c >>> 24)        ).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>> 18) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>> 12) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>>  6) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ( c         & 0x3f)).toString(16).toUpperCase();
	} else if (c < 0x80000000) {
		return '.' + (0xfc |  (c >>> 30)        ).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>> 24) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>> 18) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>> 12) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ((c >>>  6) & 0x3f)).toString(16).toUpperCase() +
			'.' + (0x80 | ( c         & 0x3f)).toString(16).toUpperCase();
	}
}

function normaliseAnchor(anchor) {
	// "." is not escaped!
	return anchor.replace(/[^0-9A-Za-z_:\.]/g, function (m) { /* [\ud800-\udbff][\udc00-\dfff]| */
		if (m.length === 2) { // surrogate pair
			return encodeCodePoint((m.charCodeAt(0) & 0x3ff) << 10 | m.charCodeAt(1) & 0x3ff);
		} else {
			return encodeCodePoint(m.charCodeAt(0));
		}
	});
}

function normaliseTitle(title) {
	try {
		var t = new mw.Title(title);
		return t.getPrefixedText();
	} catch (e) {
		return null;
	}
}

function el(tag, child, attr, events) {
	var node = document.createElement(tag);
 
	if (child) {
		if ((typeof child === 'string') || (typeof child.length !== 'number'))
			child = [child];
		for (var i = 0; i < child.length; ++i) {
			var ch = child[i];
			if ((ch === void(null)) || (ch === null))
				continue;
			else if (typeof ch !== 'object')
				ch = document.createTextNode(String(ch));
			node.appendChild(ch);
		}
	}

	if (attr) for (var key in attr) {
		if ((attr[key] === void(0)) || (attr[key] === null))
			continue;
		node.setAttribute(key, String(attr[key]));
	}

	if (events) for (var key in events) {
		var handler = events[key];
		if ((key === 'input') && (window.oninput === void(0))) {
			key = 'change';
		}
		node.addEventListener(key, handler, false);
	}

	return node;
}

function link(child, href, attr, ev) {
	attr = attr || {};
	ev = ev || {};
	if (typeof attr === 'string') {
		attr = { "title": attr };
	}
	if (typeof href === 'string')
		attr.href = href;
	else {
		attr.href = 'javascript:void(null);';
		ev.click = href;
	}
	return el('a', child, attr, ev);
}