MediaWiki:Gadget-StaffColorsUpdater.js

/* jshint esversion: 5, forin: true, immed: true, indent: 4, latedef: true, newcap: true, noarg: true, undef: true, undef: true, unused: true, browser: true, jquery: true, onevar: true, eqeqeq: true, multistr: true, maxerr: 999999, -W082, -W084 /* global mw */ mw.loader.using(['mediawiki.api']).then(function {   var data = {		ranks: [			'Bot',			'Bureaucrat',			'Administrator',			'Code Editor',			'Content Moderator',			'Discussions Moderator',			'Rollback',		],		imageUrls: {			Bot: 'https://static.wikia.nocookie.net/hypixel-skyblock/images/2/27/Badge-Bureaucrat.png/revision/latest/scale-to-width-down/16',			Bureaucrat: 'https://static.wikia.nocookie.net/hypixel-skyblock/images/2/27/Badge-Bureaucrat.png/revision/latest/scale-to-width-down/16',			Administrator: 'https://static.wikia.nocookie.net/hypixel-skyblock/images/9/95/Badge-Admin.png/revision/latest/scale-to-width-down/16',			'Code Editor': 'https://static.wikia.nocookie.net/hypixel-skyblock/images/9/95/Badge-CodeEditor.png/revision/latest/scale-to-width-down/16',			'Content Moderator': 'https://static.wikia.nocookie.net/hypixel-skyblock/images/0/03/Badge-ContentModerator.png/revision/latest/scale-to-width-down/20', 'Discussions Moderator': 'https://static.wikia.nocookie.net/hypixel-skyblock/images/a/aa/Badge-DiscussionsModerator.png/revision/latest/scale-to-width-down/20', Rollback: 'https://static.wikia.nocookie.net/hypixel-skyblock/images/6/68/Badge-Rollback.png/revision/latest/scale-to-width-down/16', },

wallText: { 'Bot': 'Bot', 'Bureaucrat': 'Bureaucrat', 'Administrator': 'Administrator', 'Code Editor': 'Code Editor', 'Content Moderator': 'Content Moderator', 'Discussions Moderator': 'Discussions Moderator', 'Rollback': 'Rollbacker', },

abbr: { 'Bot': "B", 'Bureaucrat': 'BU', 'Administrator': 'AD', 'Code Editor': 'CE', 'Content Moderator': 'CM', 'Discussions Moderator': 'DM', 'Rollback': 'RB', },

ids: { 'bot': 'Bot', 'bureaucrat': 'Bureaucrat', 'sysop': "Administrator", 'codeeditor': 'Code Editor', 'content-moderator': 'Content Moderator', 'threadmoderator': 'Discussions Moderator', 'rollback': 'Rollback', },

order: { 'Bot': 7, 'Bureaucrat': 6, 'Administrator': 5, 'Code Editor': 4, 'Content Moderator': 3, 'Discussions Moderator': 2, 'Rollback': 1, },

colors: { Bot: ['darkgray', 'gray'], Bureaucrat: ['#ff3f43', '#ff3f43'], Administrator: ['#38f689', '#38f689'], 'Code Editor': ['#5874f3', '#5265ba'], 'Content Moderator': ['#7FFFD4', '#397561'], 'Discussions Moderator': ['#1f9921', '#648264'], Rollback: ['#ff992b', '#a36726'], },

selectors: { 'LINKS': 'a[href$="$1"],', 'TAGS': 'a[href$="$1"][class^="EntityHeader_name"]:after,', 'ICONS': '*:not(.wds-avatar) > a[href$="User:$1"]::before,', },

styles: { TAGS: { 'content': "\"$1\" !important", 'font': "small-caps normal 100% arial !important", 'text-shadow': "none !important", 'margin-left': "10px", 'font-size': "13px !important", },

LINKS: { 'font-weight': 'bold !important', 'color': '$1 !important', 'text-shadow': '0 0 4px $2 !important', },

ICONS: { content: "\" \"", background: "url($1) no-repeat center", padding: "8px", 'margin-right': "2px", },		},

implicitGroups: ['autoconfirmed', 'user', '*'], };

function parse { var def = new $.Deferred;

new mw.Api.get({			action: "query",			list: "allusers",			augroup: Object.keys(data.ids).join('|'),			auprop: "groups",			aulimit: 500,		}).done(function(d) {			var ret = ['/* Staff Colors\n\nThis stylesheet contains the css to color staff member\'s names. \nIt is automatically updated, any changes you make will be \noverwritten next time this stylesheet gets updated. \n*/'];			d = d.query.allusers				.map(function(v) { return { name: v.name, groups: v.groups .filter(function(v) { return !data.implicitGroups.includes(v); }) .map(function(v) { return data.ids[v]; }), };				});

var groupsList = {}; data.ranks.forEach(function(rank) {				groupsList[rank] = [];				d.forEach(function(v) { if (v.groups.indexOf(rank) !== -1) { groupsList[rank].push(v.name); }				});			});

function objectToRule(o) { var ret = []; Object.keys(o).forEach(function(k) {					ret.push('\t' + k + ': ' + o[k] + ';');				}); return '{\n' + ret.join('\n') + '\n}'; }

['LINKS', 'ICONS', "TAGS"].forEach(function(state, i) {				ret.push("/*** " + state + " ***/\n");

var temp = []; var done = {}; Object.keys(groupsList).forEach(function(rank) {					var users = groupsList[rank];					var hidden = [];					var userList = [];

temp.push("\n/* " + rank + "*/");

users.forEach(function(user) {						var sel = data.selectors[state].replace(/\$1/, user);

if (!done[user]) userList.push(sel); else hidden.push(' ' + sel);

done[user] = true; });

temp.push('/* ' + data.abbr[rank] + '\'s With higher ranks are removed\n' + hidden.join('\n') + '\n*/'); temp.push(userList.join('\n'));

var rule = objectToRule(data.styles[state]); if (i === 0) rule = rule.replace(/\$1/, data.colors[rank][0]).replace(/\$2/, data.colors[rank][1]); else if (i === 1) rule = rule.replace(/\$1/, data.imageUrls[rank]); else if (i === 2) rule = rule.replace(/\$1/, data.wallText[rank]);

var len = temp.push(rule); temp[len-2] = temp[len-2].replace(/,(\s*)$/, '$1'); });				ret.push(temp.join('\n'));			});

def.resolve(ret.join('\n')); });

return def; }

(function init {		if ( window.StaffColorsUpdaterLoaded || mw.config.get('wgPageName') !== 'MediaWiki:Custom-common.less/staff-colors.less' || mw.config.get('wgAction') !== 'view' || !/sysop|codeeditor|staff|util|soap|wiki-manager|content-team-member|content-reviewer|content-volunteer/.test(mw.config.get('wgUserGroups').join('\n').toLowerCase) ) return;		window.StaffColorsUpdaterLoaded = true;		$('#mw-content-text').prepend($(' ', {			class: "wds-button",			html: $(' ', { click: function { $(this).text("Updating..."); $(this).attr({ disabled: true });

parse.then(function(css) {						new mw.Api.postWithEditToken({ action: "edit", text: css, title: mw.config.get('wgPageName'), summary: "Updating Staff Colors (StaffColorsUpdater)", minor: 1, }).then(function { $(this).text("Update Staff Colors"); alert('Done!'); }.bind(this));					}.bind(this)); },				text: "Update Staff Colors", }),		}));	}); });