User:Microchip08/highlight.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * highlight.js
 * Author: [[w:pl:User:Beau]]
 * 
 * Adds CSS to links (<a>) on all pages for
 * administrators, bots, and stewards.
 * 
 * Adds:
 * .nick_sysop to administrators
 * .nick_bot to flagged bots
 * .nick_stewards to stewards on all wikis
 * .nick_editor to editors
 * 
 * You can view [[m:User:Microchip08/global.css]] for example CSS.
 * 
 * Limitations:
 * On large wikis, where there are a considerable number
 * of each user group, only the first 500 will be appropriately tagged.
 */
if ( typeof( cn$replacement ) == "undefined" ) {
	window.cn$replacement = {};
}

if ( typeof( cn$class ) == "undefined" ) {
	window.cn$class = {};
}

window.coloredNicknamesGadget = {
	queries: 0,
	loaded: false,
	cache: {},
	list: {},
	version: 10,
	userLink: /(?:Wiki(?:pedystk?a|skryba|reporter)|U.+ytkownik|U.+ytkowniczka|User)/,
	contribLink: /Specjalna:Wk/,

	init: function() {
		var that = this;
        var wgServer = mw.config.get('wgServer');
		var groups = [
			'bot',
			'sysop'
		];
		if ( wgServer == '//en.wikinews.org' ) {
			groups.push( 'editor' );
		} else if ( wgServer == '//commons.wikimedia.org') {
			groups.push( 'OTRS-member' );
		}
		if ( wgServer == '//meta.wikimedia.org' ) {
			groups.push( 'steward' );
		} else {
			// to get around same origin policy
			this.queries++;
			jQuery.ajax( {
				url: '//meta.wikimedia.org/w/api.php',
				dataType: 'jsonp',
				async: false,
				data: {
					action: 'query',
					list: 'allusers',
					augroup: 'steward',
					aulimit: 'max',
					maxage: 43200,
					smaxage: 600,
					format: 'json'
				},
				success: function ( result ) {
					that.addToList( result, 'steward' );
				}
			} );
		}
		for (var i = 0; i < groups.length; i++) {
			var group = groups[i];
			var request = {
				action: 'query',
				list: 'allusers',
				augroup: group,
				aulimit: 'max',
				maxage: 43200, 
				smaxage: 600,
				format: 'json'
			};
			this.queries++;
			jQuery.ajax({
				url: mw.util.wikiScript( 'api' ),
				dataType: 'json',
				async: false, // as group does not get preserved
				data: request,
				success: function( result ) {
					that.addToList( result, group );
				}
			} );
		}
		
		jQuery( document ).ready( function() {
			that.loaded = true;
			if ( that.queries === 0 ) {
				that.doColor();
			}
		} );
	},
	addToList: function( data, type ) {
		if ( data ) {
			for ( var id in data.query.allusers ) {
				var nick = data.query.allusers[id].name;
				if ( this.list[nick] ) {
					this.list[nick].push( type );
				} else {
					this.list[nick] = new Array( type );
				}
				console.log( nick + ' is a ' + type );
			}
		}
		this.queries--;
		if ( this.queries === 0 && this.loaded ) {
			this.doColor();
		}
	},
	getUserClass: function( nick ) {
		if ( nick === null ) {
			return [];
		}

		var userClass = this.cache[nick];
		if ( userClass ) {
			return userClass;
		}
		userClass = [];

		var nc = cn$class[nick];
		if ( nc ) {
			userClass.push( nc );
		}

		if ( this.list[nick] ) {
			userClass = userClass.concat( this.list[nick] );
		}

		this.cache[nick] = userClass;

		return userClass;
	},

	doColor: function() {
		this.queries = -1;

		var links = document.getElementsByTagName( 'a' );

		for ( var i = 0; i < links.length; i++ ) {
			var link = links[i];
			if ( !link.href.match( this.userLink ) && !link.href.match( this.contribLink ) ) {
				continue;
			}
			var nick = jQuery( link ).text();

			var replacement = cn$replacement[nick];
			if ( replacement ) {
				link.innerHTML = replacement;
			}

			var userClass = this.getUserClass( nick );
			if ( userClass.length ) {
				link.className += ' nick_' + userClass.join( ' nick_' );
				link.title += ' (' + userClass.join( ' and ' ) + ')';
			}
		}

		this.cache = {};
	}
};

coloredNicknamesGadget.init();