MediaWiki:InterwikiRC/code.js

/** * Name:       InterwikiRC * Description: Automatically updates interwiki links on RecentChanges in a set interval of time * Author:     KockaAdmiralac <1405223@gmail.com> * Version:    v1.1 */ (function {   'use strict';    if( mw.config.get('wgCanonicalSpecialPageName') !== 'Recentchanges' || window.InterwikiRCLoaded ) {       return;    }    window.InterwikiRCLoaded = true;    var InterwikiRC = {        config: $.extend({ interval: 7200000 }, window.InterwikiRCConfig),       map: [],        preload: function {            $.get('/load.php', { mode: 'articles', articles: 'u:pl.quentum:MediaWiki:Custom-language-code-sorting', only: 'styles' }, $.proxy(this.init, this));       },        init: function(d) {            this.codes = JSON.parse(d.replace(/\/\*.*\*\//g, ''));            this.api = new mw.Api;            this.refreshing = $(' ', { id: 'InterwikiRCRefreshing', src: 'https://vignette1.wikia.nocookie.net/dev/images/c/c5/Circle_throbber.gif', width: 16, height: 16 }).hide.appendTo('.page-header__main');           setInterval($.proxy(this.updateLanguages, this), this.config.interval);            this.checkCache;        },        checkCache: function {            var cache = localStorage.getItem('InterwikiRC');            if(cache) {                cache = JSON.parse(cache);                this.map = cache.data;                this.showLanguages;                if(new Date - new Date(cache.updated) > this.config.interval) {                    this.updateLanguages;                }            } else {                this.updateLanguages;            }        },        updateLanguages: function {            this.map = [];            this.refreshing.show;            this.api.get({ action: 'query', meta: 'siteinfo', siprop: 'interwikimap', sifilteriw: 'local' }).done($.proxy(this.cbLanguages, this));       },        cbLanguages: function(d) {            if(!d.error) {                this.tempMap = d.query.interwikimap.filter(function(obj) { return obj.language; });               this.checkNextWiki;            }        },        checkNextWiki: function {            this.checkWiki = this.tempMap.shift;            if(this.checkWiki) {                $('#InterwikiRCCheck').remove;                var script = $(' ', { src: this.checkWiki.url.replace(                       '/wiki/$1',                        '/api.php?action=query&meta=siteinfo&format=json&callback='                    ), type: 'text/javascript', id: 'InterwikiRCCheck' })[0];               script.addEventListener('load', $.proxy(this.cbCheckWiki, this));                script.addEventListener('error', $.proxy(this.checkNextWiki, this));                document.head.appendChild(script);            } else {                localStorage.setItem('InterwikiRC', JSON.stringify({                    updated: new Date,                    data: this.map                }));                this.showLanguages;            }        },        cbCheckWiki: function {            this.map.push(this.checkWiki);            this.checkNextWiki;        },        showLanguages: function {            this.refreshing.hide;            var $list1 = $('.page-header__languages .wds-list'),                $list2 = $('.WikiaArticleInterlang ul');            $list1.html();            $list2.html();            this.map.sort(function(a, b) { return a.language.localeCompare(b.language); }).forEach(this.forEachGen($list1));           this.map.sort($.proxy(function(a, b) {                return this.codes.indexOf(a.prefix) -                       this.codes.indexOf(b.prefix);            }, this)).forEach(this.forEachGen($list2));        },        forEachGen: function($list) {            return function(el) {                $list.append( $('').append(                       $('', { href: el.url.replace('$1', 'Special:RecentChanges'), text: el.language })                   )                );            };        }    };    mw.loader.using('mediawiki.api').then($.proxy(InterwikiRC.preload, InterwikiRC)); });