MediaWiki:Common.js

/* Any JavaScript here will be loaded for all users on every page load. */ /** * Name:       DiscordIntegrator * Author:     KockaAdmiralac <1405223@gmail.com> * Description: Allows intergration with Discord * TODO:       I18N */ (function {   'use strict';    var mconfig = mw.config.get([ 'wgUserLanguage', 'wgUserName' ]);   if (window.DiscordIntegratorLoaded) {        return;    }    window.DiscordIntegratorLoaded = true;    /**     * Main object     * @static     */    var DiscordIntegrator = {        /**         * Configuration for the plugin         * @property config         * @type {Object}         */        config: (window.DiscordIntegratorConfig || {}).siderail || {},        /**         * Preload resources         */        preload: function {            mw.hook('wikipage.content').add($.proxy(this.insertToContent, this));            mw.loader.using('mediawiki.api').then($.proxy(function {                this.api = new mw.Api;                this.api.get({ action: 'query', meta: 'allmessages', ammessages: [ 'id', 'title', 'moduleHeight', 'theme', 'width', 'height', 'text', 'logged-in', 'footer', 'username' ].map(function(el) {                       return 'Custom-DiscordIntegrator-config-' + el;                    }).join('|'), amlang: mconfig.wgUserLanguage, smaxage: 300, maxage: 300 }).done($.proxy(function(d) {                   if (!d.error) {                        d.query.allmessages.forEach(function(el) { if(el['*']) { this.config[el.name.substring(32)] = el['*']; }                       }, this);                        this._loading = 0;                        ['text', 'title', 'footer'].forEach(this.parse, this);                        if (this._loading === 0) {                            this.init;                        }                    }                }, this));            }, this));        },        /**         * Parse the configuration that needs to be parsed         */        parse: function(msg) {            if (this.config[msg]) {                ++this._loading;                this.api.get({ action: 'parse', text: this.config[msg] }).done($.proxy(function(d) {                   if (!d.error) {                        this.config[msg] = d.parse.text['*'];                        if (--this._loading === 0) {                            this.init;                        }                    }                }, this));            }        },        /**         * Initializing         */        init: function {            if (this.config.id && $('#WikiaRail').length > 0) {                var clas = $('#WikiaRail').attr('class');                if (clas && clas.split(/\s+/).indexOf('loaded') === -1) {                    $('#WikiaRail').on('afterLoad.rail', $.proxy(this.insertToSiderail, this));                } else {                    this.insertToSiderail;                }            }        },        /**         * Inserting the widget to siderail         */        insertToSiderail: function {            var filter = $('#top-right-boxad-wrapper, #NATIVE_TABOOLA_RAIL, .content-review-module').last, // TODO: Insert some user configuration here el = $(' ', { class: 'DiscordIntegratorModule rail-module' }); if (this.config.title) { el.append(                   $(' ', { 'class': 'activity-heading', html: this.config.title })               );            }            if (this.config.text) { el.append(                   $(' ', { id: 'DiscordIntegratorModuleText', html: this.config.text })               );            }            el.append(this.generateContent(this.config)); if(this.config.footer) { el.append(                   $(' ', { id: 'DiscordIntegratorModuleFooter', html: this.config.footer })               );            }            if (filter.length > 0) { el.insertAfter(filter); } else { $('#WikiaRail').prepend(el); }           if (this.config.moduleHeight) { mw.util.addCSS('.DiscordIntegratorModule { height: ' + Number(this.config.moduleHeight) + 'px; }'); }           mw.hook('DiscordIntegrator.added').fire(el); },       /**         * Finding the designated places in content * in which to place the widget and placing it        */ insertToContent: function($content) { $content.find('.DiscordIntegrator:not(.loaded)').each($.proxy(function(cabbage, el) { el = $(el); el.html(this.generateContent(el.data)) .addClass('loaded'); }, this)); },       /**         * Generating widget content from an object * @todo i18n * @return [String] content of the widget */       generateContent: function(config) { return config.id ? ((config.loggedIn === true || Boolean(config['logged-in']) === true) && !mconfig.wgUserName) ? 'Please log in to see this widget' : mw.html.element('iframe', {               src: 'https://discordapp.com/widget?id=' +                     config.id +                     '&theme=' +                     (config.theme === 'light' ? 'light' : 'dark') +                    '&username=' + encodeURIComponent( config.username === '@disabled' ? '' :                        config.username === '@function' && typeof window.DiscordIntegratorGetUsername === 'function' ? window.DiscordIntegratorGetUsername : config.username || mconfig.wgUserName ),               width: config.width || '100%',                height: config.height || '400px',                allowtransparency: 'true',                frameborder: '0'            }) : 'Error: ID of the widget is not supplied'; }   };    $($.proxy(DiscordIntegrator.preload, DiscordIntegrator)); });