Jump to content

User:Evad37/ScriptModules.js

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by Evad37 (talk | contribs) at 07:43, 27 January 2019 (allow multiple modules to be specified in an array). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
Window.exportScriptModule = function(name, exported) {
	Window.exportScriptModule[name] = exported;
};
Window.importScriptModule = function(module) {
	return mw.loader.using('mediawiki.util').then(function() {
		var modules = Array.isArray(module) ? module : [module];
		var exports = modules.map(function(name) {
			var alreadyLoaded = ( Window.exportScriptModule[name] !== undefined );
			return $.when(
				alreadyLoaded ||
				$.getScript(
					// When done for real, url would have `title=MediaWiki:Module/`
					'https://en.wikipedia.org/w/index.php?title=User:Evad37/Module/' +
					mw.util.wikiUrlencode(name) +
					 '.js&action=raw&ctype=text/javascript'
				)
			)
			.then(
				function() {
					if ( Window.exportScriptModule[name] === undefined ) {
						throw new Error('Script module "' + name + '" did not export anything!');
					}
					return Window.exportScriptModule[name];
				},
				function() {
					throw new Error('Failed to load script module "' + name + '"');
				}
			);
		});
		return $.when.apply(null, exports).promise();
	});
};