Jump to content

User:Evad37/ScriptModules.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 07:50, 26 January 2019 (pass array to load multiple modules at once via recursion; adjust error messages). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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() {
     	if ( Array.isArray(module) ) {
     		var imports = module.map(function(name) {
 				return Window.importScriptModule(name);
 			});
     		return $.when.apply(null, imports);
     	}
     	
        if ( Window.exportScriptModule[module] === undefined ) {
            // Load module
            return $.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(module) +
                '.js&action=raw&ctype=text/javascript'
            );
        }
        // else module has already been loaded
        return true;
    })
    .then(
    	function() {
        	if ( Window.exportScriptModule[module] === undefined ) {
            	throw new Error('Script module "' + module + '" did not export anything!');
        	}
        	return Window.exportScriptModule[module];
    	},
    	function() {
        	 throw new Error('Failed to load script module "' + module + '"');
    	}
    );
};