MediaWiki:Highlight.js

/** * Name:       HighlightUpdate * Version:    v1.0 * Author:     KockaAdmiralac <1405523@gmail.com> * Description: Adds a button for updating MediaWiki:Highlight.css. */ require([   'wikia.window',    'jquery',    'mw',    'BannerNotification' ], function(window, $, mw, BannerNotification) {    'use strict';    var config = mw.config.get([ 'wgArticleId', 'wgCityId' ]);   if ( config.wgArticleId !== 2160 || config.wgCityId !== '7931' || $('#ca-highlights').exists ) {       return;    }    var HighlightUpdater = {        groups: {            'content-volunteer': '#ff7000',            'vanguard': '#1eaf7a',            'voldev': '#23c8d2',            'global-discussions-moderator': '#4286f4',            'vstf': '#ff7777',            'helper': '#4c5f1d',            'staff': '#ddaa00'        },        init: function {            this.api = new mw.Api;            $('.page-header__contribution-buttons .wds-list').append( $('').append(                   $('', { click: $.proxy(this.click, this), id: 'ca-highlights', text: 'Update highlights' })               )            );        },        click: function {            this.api.get({ action: 'query', list: 'groupmembers', gmgroups: Object.keys(this.groups).join('|'), gmlimit: 'max', pageids: 2160, prop: 'revisions', rvprop: 'content', indexpageids: true, cb: Date.now }).then($.proxy(this.callback, this));       },        callback: function(data) {            var users = {};            for (var group1 in this.groups) {                users[group1] = [];            }            data.users.forEach(function(u) { u.groups.forEach(function(g) {                   users[g].push(u.name);                }); });           var text = '';            for (var group in users) {                text +=                    '/* ' + group + ' */\n' +                    this.getSelectorsFor(group, users[group].sort)                    .join(',\n') + ' {\n    color: ' + this.groups[group] +                    ' !important;\n}\n\n';            }            this.api.post({ action: 'edit', text: data.query.pages[data.query.pageids[0]] .revisions[0]['*'] .replace(                       /(\/\* HighlightUpdate-start \*\/\n)[\s\S]*$/igm,                        function(_, m) {                            return m + text;                        }                    ), title: 'MediaWiki:Highlight.css', // eslint-disable-next-line max-len summary: 'Automatically updating via Highlight.js', minor: true, bot: true, token: mw.user.tokens.get('editToken') }).then($.proxy(this.edited, this));       },        getSelectorsFor: function(group, users) {            var encode = [];            users.forEach(function(u) { var regularEncode = u.replace(/\s/g, '_'), wikiEncode = mw.util.wikiUrlencode(u); encode.push(regularEncode); if (regularEncode !== wikiEncode) { encode.push(wikiEncode); }           });            var selectors = encode.map(function(sel) { return 'a[href$=":' + sel + '"]'; });           if (group === 'staff') {                selectors.push('.ChatWindow .User.staff');            }            return selectors;        },        edited: function {            new BannerNotification( 'Highlights successfully updated!', 'confirm' ).show;           setTimeout(function { window.location.reload; }, 3000);       }    };    mw.loader.using('mediawiki.api')        .then($.proxy(HighlightUpdater.init, HighlightUpdater)); });