Jump to content

User:Evad37/ScriptModules.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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();
	});
};