MediaWiki:Fetch.js

/** * @Name           Fetch * @Version        v1.0 * @Author         TheGoldenPatrik1 * @Description    Library for fetching and using system messages. */   window.dev = window.dev || {}; if (window.dev.fetch !== undefined) { return; }   /**     * @method fetch * @description Gets the message(s) and returns them * @param {String|Array} msg - The message(s) to get * @param {Function} done - The function to perform afterwards * @param {String} lang - The API request language (optional) * @throws {Error} when no messages are specified * @throws {Error} when no done function is specified * @throws {Error} when the API request fails */   function fetch (msg, done, lang) { if (!msg) { throw new Error('Please specify a message to get!'); } else if (!done) { throw new Error('Please specify a function to load!'); }       var arr; if (Array.isArray(msg)) { arr = msg; msg = msg.join('|'); }       new mw.Api.get({            action: 'query',            meta: 'allmessages',            ammessages: msg,            amlang: lang || mw.config.get('wgUserLanguage')        }) .done(function (d) {           if (d.error) {                throw new Error( 'Failed to get messages: ' + d.error.code + '!' );           } else {                var data = d.query.allmessages;                if (msg.includes('|')) {                    done(function (v) { if (typeof v === 'number') { return data[v]['*']; } else { var mo = {}; $.each(arr || msg.split('|'), function (i, k) {                               mo[k] = data[i]['*'];                            }); return mo[v]; }                   });                } else {                    done(data[0]['*']);                }            }        }) .fail(function {            throw new Error('Failed to get messages!');        }); }   /**     * @method init * @description Fires the hook and defines window.dev.fetch */   function init  { window.dev.fetch = fetch; mw.hook('dev.fetch').fire(window.dev.fetch); }   mw.loader.using('mediawiki.api').then(init); })(this, jQuery, mediaWiki);
 * (function (window, $, mw) {