User:Thundercraft5/global.js/modules.js

/* jshint esversion: 6, forin: true, immed: true, indent: 4, latedef: true, newcap: true, noarg: true, undef: true, undef: true, unused: true, browser: true, jquery: true, onevar: true, eqeqeq: true, multistr: true, maxerr: 999999, -W082, -W084 /* global mw */ var loader = { require(name) { if (Array.isArray(name) || arguments.length > 1) { var ret = []; (arguments.length > 1 ? arguments : name).forEach(v => ret.push(this.require(v))); return ret; } else { var ret = this.registry[this.findPath(name) || name]; if (!ret) throw new TypeError('Module "' + name + '" was not found.');

var exports = {}; name = name.replace('(?:\.(?:js|javascript))$', '');

function localRequireFunc(module) { var name2 = name?.replace(/\/[^\\/]+?$/, '') // jshint ignore:line return this.require(module?.replace(/^\.\//, name + "/")) || null; // jshint ignore:line }			if (useCache && this.cache[name]) return this.cache[name]; try { return ret(localRequireFunc.bind(this), exports, name) || exports; } catch(e) { this.log('Exception in executing module "' + name + '":'); console.warn(e); }		}	},	normalizePath(parentId, moduleName) { if (moduleName.charAt(0) === ".") { var base = parentId.split("/").slice(0, -1).join("/"); moduleName = base + "/" + moduleName; while (moduleName.indexOf(".") !== -1 && previous !== moduleName) { var previous = moduleName; moduleName = moduleName.replace(/\/\.\//, "/").replace(/[^\/]+\/\.\.\//, ""); }		}		return moduleName; },	findPath(path, root) { path = path?.replaceAll(/\@(\w+)/g, (_, $1) => this.getAlias($1)); // jshint ignore:line

return Object.keys(this.registry).find(v => {			var regex = new RegExp('^' + String(root ? this.normalizePath(root || '', path) : path) .replace(/^\.\//, root || '') .replace(/\.{2}/g, '.+?') + '(?:\.(?:js|javascript))?$' );			return regex.test(v);		}) || null; },	register(name, module) { return this.registry[name] = module; // jshint ignore:line },	initiate { var def = new $.Deferred;

this.api.get({			action: "query",			list: "allpages",			apprefix: "Thundercraft5/global.js/",			apnamespace: 2,			aplimit: 500,		}).then(d => {			this.api.get({ action: 'query', cb: Date.now, format: 'json', indexpageids: true, prop: 'revisions', rvprop: 'content', titles: d.query.allpages.map(v => v.title).join('|'), })			.done(def.resolve)			.catch(console.warn);		}).catch(console.warn); },	prepareAPIData(data) { var ids = data.query.pageids, pages = data.query.pages, o = {}; ids.forEach(function(id) {			var currentData = pages[id],				title = currentData.title.replace('User:Thundercraft5/global.js/', ).replace('.js$', );			if (!currentData.revisions) return this.warn('The requested module "' + title + '" does not exist, skipping.');			var content = currentData.revisions[0]["*"];			o[title] = {				content: content,				id: id,			};		}.bind(this)); return o;	}, setInCache(scripts, content) { if (typeof(scripts) !== "object") return mw.storage.set('loader-script-cache-' + scripts, content); Object.keys(scripts).forEach(v => {			mw.storage.set('loader-script-cache-' + v, v.content);		}); },	hasInCache(name) { return !!mw.storage.get('loader-script-cache-' + name); },	getFromCache(name) { return mw.storage.get('loader-script-cache-' + name); },	executeImmediate(js, scriptName, id) { var script = document.createElement('script'); script.textContent = js; script.type = 'text/javascript'; script.setAttribute('data-script-name', script); script.class = 'loadModules-Module'; this.log('Executing script "' + scriptName + '" (script id #' + id + ')'); document.head.append(script); },	listModules { return Object.keys(this.registry); },	getModules { return this.registry; },	registerBulk(prefix, modules) { for (const i in modules) { if (modules.hasOwnProperty(i)) this.register(prefix + '/' + i, modules[i]); }		return modules; },	unregister(name) { if (Array.isArray(name) || arguments.length > 1) { var ret = []; (arguments.length > 1 ? arguments : name).forEach(v => ret.push(this.unregister(v))); return ret; } else { name = this.findPath(name) || name;

if (!(name in this.registry)) return this.warn('failed to unregister module "' + name + '": module was not found.'); var v = this.registry[name]; delete this.registry[name];

return v;		} },	unregisterBulk(prefix, modules) { var ret = []; for (var i in modules) { if (modules.hasOwnProperty(i)) ret.push(this.unregister(prefix + '/' + i)); }		return ret; },	setAlias(alias, value) { return this.aliases[alias.toLowerCase] = value; // jshint ignore:line },	getAlias(alias) { var v = this.aliases[alias.toLowerCase]; if (!v) return this.warn('Path alias "' + alias + '" was not found.'), null; return v;	}, hasAlias(alias) { return !!this.aliases[alias.toLowerCase]; },

removeAlias(alias) { return delete this.aliases[alias.toLowerCase]; },

logBuilder(level, args) { var args = Array.from(args); args.unshift('[ModuleManager] [' + level.toUpperCase + ']:'); console[level].apply(null, args); return args.join(' '); },

log { return this.logBuilder('log', arguments); },

warn { return this.logBuilder('warn', arguments); },

api: new mw.Api({	   ajax: {	        url: 'https://ucp.fandom.com/api.php',	        xhrFields: {	            withCredentials: true	        },	        dataType: 'JSONP',	        crossDomain: true	    }	}), aliases: {}, registry: {}, cache: {}, };