MediaWiki:Gadget-rsw-util.js

(function ($, mw, rs) {

'use strict'; /**    * Polyfills **/   // Polyfill for window.location.origin being non-existent on IE9 if (!window.location.origin) { window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: ''); }	function createOOUIWindowManager { if (window.OOUIWindowManager == undefined) { window.OOUIWindowManager = new OO.ui.WindowManager; $( 'body' ).append( window.OOUIWindowManager.$element ); }   	return window.OOUIWindowManager; }   /**     * Reusable functions *    * These are available under the `rswiki` global variable. * @example `rswiki.addCommas` * The alias `rs` is also available in place of `rswiki`. */   var util = { /**        * Formats a number string with commas. *        * @todo fully replace this with Number.protoype.toLocaleString *      > 123456.78.toLocaleString('en') *        * @example 123456.78 -> 123,456.78 *        * @param num {Number|String} The number to format. * @return {String} The formated number. */       addCommas: function (num) { if (typeof num === 'number') { return num.toLocaleString('en'); }

// @todo chuck this into parseFloat first and then to toLocaleString? num += '';

var x = num.split('.'), x1 = x[0], x2 = x.length > 1 ? '.' + x[1] : '',               rgx = /(\d+)(\d{3})/;

while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1,$2'); }

return x1 + x2; },

/**        * Extracts parameter-argument pairs from templates. *        * @todo Fix for multiple templates *        * @param tpl {String} Template to extract data from. * @param text {String} Text to look for template in. * @return {Object} Object containing parameter-argument pairs */       parseTemplate: function (tpl, text) { var rgx = new RegExp(                   '\\{\\{(template:)?' + tpl.replace(/[ _]/g, '[ _]') + '\\s*(\\||\\}\\})',                    'i'                ), exec = rgx.exec(text), // splits template into |arg=param or |param paramRgx = /\|(.*?(\{\{.+?\}\})?)(?=\s*\||$)/g, args = {}, params, i,               j;

// happens if the template is not found in the text if (exec === null) { return false; }

text = text.substring(exec.index + 2);

// used to account for nested templates j = 0;

// this purposefully doesn't use regex // as it became very difficult to make it work properly for (i = 0; i < text.length; i += 1) { if (text[i] === '{') { j += 1; } else if (text[i] === '}') { if (j > 0) { j -= 1; } else { break; }               }            }

// cut off where the template ends text = text.substring(0, i); // remove template name as we're not interested in it past this point text = text.substring(text.indexOf('|')).trim; // separate params and args into an array params = text.match(paramRgx);

// handle no params/args if (params !== null) { // used as an index for unnamed params i = 1;

params.forEach(function (el) {                   var str = el.trim.substring(1),                        eq = str.indexOf('='),                        tpl = str.indexOf('{{'),                        param,                        val;

// checks if the equals is after opening a template // to catch unnamed args that have templates with named args as params if (eq > -1 && (tpl === -1 || eq < tpl)) { param = str.substring(0, eq).trim.toLowerCase; val = str.substring(eq + 1).trim; } else { param = i;                       val = str.trim; i += 1; }

args[param] = val; });           }

return args; },

/**        * Alternate version of `parseTemplate` for parsing exchange module data. *        * @notes Only works for key-value pairs *        * @param text {String} Text to parse. * @return {Object} Object containing parameter-argument pairs. */       parseExchangeModule: function (text) {

// strip down to just key-value pairs var str = text .replace(/return\s*\{/, '') .replace(/\}\s*$/, '') .trim, rgx = /\s*(.*?\s*=\s*(?:\{[\s\S]*?\}|.*?))(?=,?\n|$)/g, args = {}, params = str.match(rgx);

if (params !== null) { params.forEach(function (elem) {                   var str = elem.trim,                        eq = str.indexOf('='),                        param = str.substring(0, eq).trim.toLowerCase,                        val = str.substring(eq + 1).trim;

args[param] = val; });           }

return args; },

/**        * Helper for making cross domain requests to RuneScape's APIs. * If the APIs ever enable CORS, we can ditch this and do the lookup directly. *        * @param url {string} The URL to look up         * @param via {string} One of 'anyorigin', 'whateverorigin' or 'crossorigin'. Defaults to 'anyorigin'. *        * @return {string} The URLto use to make the API request. */       crossDomain: function (url, via) { switch (via) { case 'crossorigin': url = 'http://crossorigin.me/' + url; break;

case 'whateverorigin': url = 'http://whateverorigin.org/get?url=' + encodeURIComponent( url ) + '&callback=?'; break;

case 'anyorigin': default: url = 'http://anyorigin.com/go/?url=' + encodeURIComponent( url ) + '&callback=?'; break; }

return url; },       /**         * Returns the OOUI window manager as a Promise. Will load OOUI (core and windows) and create the manager, if necessary. *         * @return {jQuery.Deferred} A jQuery Promise where window.OOUIWindowManager is will be defined * Chaining a .then will pass OOUIWindowManager to the function argument */       withOOUIWindowManager: function { return mw.loader.using(['oojs-ui-core','oojs-ui-windows']).then(createOOUIWindowManager); },       /**         * Helper for creating and initializing a new OOUI Dialog object * After init, the window is added to the global Window Manager. *         * Will automatically load OOUI (core and windows) and create the window manager, if necessary. window.OOUIWindowManager will be defined within this. *         * @author JaydenKieran *         * @param name {string} The symbolic name of the window * @param title {string} The title of the window * @param winconfig {object} Object containing params for the OO.ui.Dialog obj * @param init {function} Function to be called to initialise the object *        * @return {jquery.Deferred} The jQuery Promise returned by mw.loader.using * Chaining a .then will pass the created {OO.ui.Dialog} object as the function argument */       createOOUIWindow: function(name, title, winconfig, init, openNow) { return mw.loader.using(['oojs-ui-core','oojs-ui-windows']).then(function{		   	createOOUIWindowManager;		    	winconfig = winconfig || {};				function myModal( config ) {					myModal.super.call( this, config );				}				OO.inheritClass( myModal, OO.ui.Dialog ); 				myModal.static.name = name;				myModal.static.title = title;				myModal.prototype.initialize = function  {					myModal.super.prototype.initialize.call( this );					init(this);				}				var modal = new myModal(winconfig);				console.debug('Adding ' + myModal.static.name + ' to WindowManager');				window.OOUIWindowManager.addWindows( [ modal ] );				if (openNow) {					window.OOUIWindowManager.openWindow(name);				}				return modal;        	}); },       /**         * Helper for checking if the user's browser supports desktop notifications * @author JaydenKieran */       canSendBrowserNotifs: function  { if (!("Notification" in window)) { console.warn("This browser does not support desktop notifications"); return false; } else { return true; }       },        /**         * Send a desktop/browser notification to a user, requires the page to be open * @author JaydenKieran *         * @param https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification *         * @return Notification object or null */       sendBrowserNotif: function (title, opts) { if (rs.canSendBrowserNotifs == false) { return null; }			Notification.requestPermission.then(function(result) {			   if (result === "granted") {			    	console.debug('Firing desktop notification');			    	var notif = new Notification(title, opts);			    	notif.onclick = function(e) {			    		window.focus;			    	}			    	return notif;			    } else {			        return null;			    }			}); },       /**         * Check if the browser has support for localStorage * @author JaydenKieran *         * @return boolean **/       hasLocalStorage: function { try { localStorage.setItem('test', 'test') localStorage.removeItem('test') return true } catch (e) { return false }       },        /**         * Check if user is using dark mode * @author JaydenKieran *         * @return boolean **/       isUsingDarkmode: function { if (typeof $.cookie('darkmode') === 'undefined') { return false } else { return $.cookie('darkmode') === 'true' }       },        /**         * Gets a query string parameter from given URL or current href * @author JaydenKieran *         * @return string or null **/        qsp: function(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, '\\$&'); var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, ' ')); },   	/**    	 * Get the URL for a file on the wiki. * @author JaydenKieran *    	 * @return string **/   	getFileURL: function(filename) { var MD5 = function(d){result = M(V(Y(X(d),8*d.length)));return result.toLowerCase};function M(d){for(var _,m="0123456789ABCDEF",f="",r=0;r>>4&15)+m.charAt(15&_);return f}function X(d){for(var _=Array(d.length>>2),m=0;m<_.length;m++)_[m]=0;for(m=0;m<8*d.length;m+=8)_[m>>5]|=(255&d.charCodeAt(m/8))<>5]>>>m%32&255);return _}function Y(d,_){d[_>>5]|=128<<_%32,d[14+(_+64>>>9<<4)]=_;for(var m=1732584193,f=-271733879,r=-1732584194,i=271733878,n=0;n<d.length;n+=16){var h=m,t=f,g=r,e=i;f=md5_ii(f=md5_ii(f=md5_ii(f=md5_ii(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_ff(f=md5_ff(f=md5_ff(f=md5_ff(f,r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+0],7,-680876936),f,r,d[n+1],12,-389564586),m,f,d[n+2],17,606105819),i,m,d[n+3],22,-1044525330),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+4],7,-176418897),f,r,d[n+5],12,1200080426),m,f,d[n+6],17,-1473231341),i,m,d[n+7],22,-45705983),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+8],7,1770035416),f,r,d[n+9],12,-1958414417),m,f,d[n+10],17,-42063),i,m,d[n+11],22,-1990404162),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+12],7,1804603682),f,r,d[n+13],12,-40341101),m,f,d[n+14],17,-1502002290),i,m,d[n+15],22,1236535329),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+1],5,-165796510),f,r,d[n+6],9,-1069501632),m,f,d[n+11],14,643717713),i,m,d[n+0],20,-373897302),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+5],5,-701558691),f,r,d[n+10],9,38016083),m,f,d[n+15],14,-660478335),i,m,d[n+4],20,-405537848),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+9],5,568446438),f,r,d[n+14],9,-1019803690),m,f,d[n+3],14,-187363961),i,m,d[n+8],20,1163531501),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+13],5,-1444681467),f,r,d[n+2],9,-51403784),m,f,d[n+7],14,1735328473),i,m,d[n+12],20,-1926607734),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+5],4,-378558),f,r,d[n+8],11,-2022574463),m,f,d[n+11],16,1839030562),i,m,d[n+14],23,-35309556),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+1],4,-1530992060),f,r,d[n+4],11,1272893353),m,f,d[n+7],16,-155497632),i,m,d[n+10],23,-1094730640),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+13],4,681279174),f,r,d[n+0],11,-358537222),m,f,d[n+3],16,-722521979),i,m,d[n+6],23,76029189),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+9],4,-640364487),f,r,d[n+12],11,-421815835),m,f,d[n+15],16,530742520),i,m,d[n+2],23,-995338651),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+0],6,-198630844),f,r,d[n+7],10,1126891415),m,f,d[n+14],15,-1416354905),i,m,d[n+5],21,-57434055),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+12],6,1700485571),f,r,d[n+3],10,-1894986606),m,f,d[n+10],15,-1051523),i,m,d[n+1],21,-2054922799),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+8],6,1873313359),f,r,d[n+15],10,-30611744),m,f,d[n+6],15,-1560198380),i,m,d[n+13],21,1309151649),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+4],6,-145523070),f,r,d[n+11],10,-1120210379),m,f,d[n+2],15,718787259),i,m,d[n+9],21,-343485551),m=safe_add(m,h),f=safe_add(f,t),r=safe_add(r,g),i=safe_add(i,e)}return Array(m,f,r,i)}function md5_cmn(d,_,m,f,r,i){return safe_add(bit_rol(safe_add(safe_add(_,d),safe_add(f,i)),r),m)}function md5_ff(d,_,m,f,r,i,n){return md5_cmn(_&m|~_&f,d,_,r,i,n)}function md5_gg(d,_,m,f,r,i,n){return md5_cmn(_&f|m&~f,d,_,r,i,n)}function md5_hh(d,_,m,f,r,i,n){return md5_cmn(_^m^f,d,_,r,i,n)}function md5_ii(d,_,m,f,r,i,n){return md5_cmn(m^(_|~f),d,_,r,i,n)}function safe_add(d,_){var m=(65535&d)+(65535&_);return(d>>16)+(_>>16)+(m>>16)<<16|65535&m}function bit_rol(d,_){return d<<_|d>>>32-_} var base = window.location.origin; filename = filename.replace(/ /g,"_") var result = MD5(filename); filename = filename.replace(/\(/g, '%28').replace(/\)/g, '%29') return base + '/f/current/' + result.substring(0,1) + '/' + result.substring(0,2) + '/' + filename; },   	isUsingStickyHeader: function { return ($('body').hasClass('wgl-stickyheader')) }   };

function init { $.extend(rs, util, {}); // add rs as a global alias window.rs = rs; }

$(init);

}(this.jQuery, this.mediaWiki, this.rswiki = this.rswiki || {}));