User:Wugapodes/CapricornUtilsDev.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:Wugapodes/CapricornUtilsDev. |
// <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);
}