User:Zabshk/common.js: Difference between revisions

From TestWiki
Content added Content deleted
mNo edit summary
No edit summary
Line 1: Line 1:
mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Zabshk/heading-icons.js&action=raw&ctype=text/javascript');
mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Zabshk/heading-icons.js&action=raw&ctype=text/javascript');


// Author: [[pl:User:Beau]]
// Original author: [[pl:User:Beau]]
// Modified and improved by: [[pl:User:Zabshk]] (I added colorizing global groups)

if ( typeof( cn$replacement ) == "undefined" ) {
if ( typeof( cn$replacement ) == "undefined" ) {
window.cn$replacement = {};
window.cn$replacement = {};
Line 20: Line 22:


init: function() {
init: function() {
var that = this;
if ( mw.config.get( 'wgNamespaceNumber' ) > -1 && mw.config.get( 'wgAction' ) != 'history' ) {

alert("/" + that.findGetParameter( 'diff' ) + "/");
if ( mw.config.get( 'wgNamespaceNumber' ) > -1 && mw.config.get( 'wgAction' ) != 'history' && that.findGetParameter( 'diff' ) != '' ) {
return;
return;
}
}

var that = this;


var request = {
var request = {
Line 54: Line 57:
} );
} );


var request = {
if ( mw.config.get( 'wgServer' ) == 'https://secure.wikimedia.org' ) {
action: 'query',
var request = {
action: 'query',
list: 'globalallusers',
list: 'allusers',
agugroup: 'steward',
augroup: 'steward',
agulimit: 'max',
aulimit: 'max',
maxage: 43200,
maxage: 43200,
smaxage: 600,
smaxage: 600,
format: 'json'
};
format: 'json'
this.queries++;
};
jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
this.queries++;
that.addCentralToList( result, 'nick_steward' );
jQuery.getJSON( '/wikipedia/meta/w/api.php', request, function( result ) {
} );
that.addToList( result, 'nick_steward' );

} );
var request = {
}
action: 'query',
list: 'globalallusers',
agugroup: 'global-sysop',
agulimit: 'max',
maxage: 43200,
smaxage: 600,
format: 'json'
};
this.queries++;
jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
that.addCentralToList( result, 'nick_global-sysop' );
} );

var request = {
action: 'query',
list: 'globalallusers',
agugroup: 'global-rollbacker',
agulimit: 'max',
maxage: 43200,
smaxage: 600,
format: 'json'
};
this.queries++;
jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
that.addCentralToList( result, 'nick_global-rollbacker' );
} );

var request = {
action: 'query',
list: 'globalallusers',
agugroup: 'global-bot',
agulimit: 'max',
maxage: 43200,
smaxage: 600,
format: 'json'
};
this.queries++;
jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
that.addCentralToList( result, 'nick_global-bot' );
} );

var request = {
action: 'query',
list: 'globalallusers',
agugroup: 'sysadmin|staff|founder|ombudsman',
agulimit: 'max',
maxage: 43200,
smaxage: 600,
format: 'json'
};
this.queries++;
jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
that.addCentralToList( result, 'nick_verified' );
} );

jQuery( document ).ready( function() {
jQuery( document ).ready( function() {
that.loaded = true;
that.loaded = true;
Line 80: Line 138:
for ( id in data.query.allusers ) {
for ( id in data.query.allusers ) {
var nick = data.query.allusers[id].name;
var nick = data.query.allusers[id].name;
if ( this.list[nick] ) {
this.list[nick].push( type );
} else {
this.list[nick] = new Array( type );
}
}
}
this.queries--;
if ( this.queries == 0 && this.loaded ) {
this.doColor();
}
},
addCentralToList: function( data, type ) {
if ( data ) {
for ( id in data.query.globalallusers) {
var nick = data.query.globalallusers[id].name;
if ( this.list[nick] ) {
if ( this.list[nick] ) {
this.list[nick].push( type );
this.list[nick].push( type );
Line 130: Line 204:


return userClass;
return userClass;
},
findGetParameter: function ( parameterName ) {
var result = null,
tmp = [];
var items = location.search.substr(1).split("&");
for (var index = 0; index < items.length; index++) {
tmp = items[index].split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
}
return result;
},
},
isDynamic: function( ip ) {
isDynamic: function( ip ) {

Revision as of 14:18, 14 April 2017

mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Zabshk/heading-icons.js&action=raw&ctype=text/javascript');

// Original author: [[pl:User:Beau]]
// Modified and improved by: [[pl:User:Zabshk]] (I added colorizing global groups)

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: /(User)/,
	contribLink: /Special:Contributions/,

	init: function() {
		var that = this;

		alert("/" + that.findGetParameter( 'diff' ) + "/");
		if ( mw.config.get( 'wgNamespaceNumber' ) > -1 && mw.config.get( 'wgAction' ) != 'history' && that.findGetParameter( 'diff' ) != '' ) {
			return;
		}

		var request = {
			action: 'query',
			list: 'allusers',
			augroup: 'sysop',
			aulimit: 'max',
			maxage: 43200,
			smaxage: 600,
			format: 'json'
		};
		this.queries++;
		jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
			that.addToList( result, 'nick_admin' );
		} );

		var request = {
			action: 'query',
			list: 'allusers',
			augroup: 'bot',
			aulimit: 'max',
			maxage: 43200,
			smaxage: 600,
			format: 'json'
		};
		this.queries++;
		jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
			that.addToList( result, 'nick_bot' );
		} );

		var request = {
			action: 'query',
			list: 'globalallusers',
			agugroup: 'steward',
			agulimit: 'max',
			maxage: 43200,
			smaxage: 600,
			format: 'json'
		};
		this.queries++;
		jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
			that.addCentralToList( result, 'nick_steward' );
		} );

		var request = {
			action: 'query',
			list: 'globalallusers',
			agugroup: 'global-sysop',
			agulimit: 'max',
			maxage: 43200,
			smaxage: 600,
			format: 'json'
		};
		this.queries++;
		jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
			that.addCentralToList( result, 'nick_global-sysop' );
		} );

		var request = {
			action: 'query',
			list: 'globalallusers',
			agugroup: 'global-rollbacker',
			agulimit: 'max',
			maxage: 43200,
			smaxage: 600,
			format: 'json'
		};
		this.queries++;
		jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
			that.addCentralToList( result, 'nick_global-rollbacker' );
		} );

		var request = {
			action: 'query',
			list: 'globalallusers',
			agugroup: 'global-bot',
			agulimit: 'max',
			maxage: 43200,
			smaxage: 600,
			format: 'json'
		};
		this.queries++;
		jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
			that.addCentralToList( result, 'nick_global-bot' );
		} );

		var request = {
			action: 'query',
			list: 'globalallusers',
			agugroup: 'sysadmin|staff|founder|ombudsman',
			agulimit: 'max',
			maxage: 43200,
			smaxage: 600,
			format: 'json'
		};
		this.queries++;
		jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function( result ) {
			that.addCentralToList( result, 'nick_verified' );
		} );

		jQuery( document ).ready( function() {
			that.loaded = true;
			if ( that.queries == 0 ) {
				that.doColor();
			}
		} );
	},
	addToList: function( data, type ) {
		if ( data ) {
			for ( 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 );
				}
			}
		}
		this.queries--;
		if ( this.queries == 0 && this.loaded ) {
			this.doColor();
		}
	},
	addCentralToList: function( data, type ) {
		if ( data ) {
			for ( id in data.query.globalallusers) {
				var nick = data.query.globalallusers[id].name;
				if ( this.list[nick] ) {
					this.list[nick].push( type );
				} else {
					this.list[nick] = new Array( type );
				}
			}
		}
		this.queries--;
		if ( this.queries == 0 && this.loaded ) {
			this.doColor();
		}
	},
	isIPv4: function( nick ) {
		return nick.match( /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ );
	},
	isIPv6: function( nick ) {
		// Source: http://home.deds.nl/~aeron/regex/
		return nick.match( /^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i );
	},
	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] );
		}

		if ( this.isIPv4( nick ) ) {
			userClass.push( 'nick_ip' );
			if ( this.isDynamic( nick ) ) {
				userClass.push( 'nick_dynamic_ip' );
			}
		} else if ( this.isIPv6( nick ) ) {
			userClass.push( 'nick_ip' );
		}
		this.cache[nick] = userClass;

		return userClass;
	},
	findGetParameter: function ( parameterName ) {
	    var result = null,
	        tmp = [];
	    var items = location.search.substr(1).split("&");
	    for (var index = 0; index < items.length; index++) {
	        tmp = items[index].split("=");
	        if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
	    }
	    return result;
	},
	isDynamic: function( ip ) {
		return false;
	},
	doColor: function() {
		this.queries = -1;

		if ( ( typeof dynamicIpsGadget ) == 'object' ) {
			this.isDynamic = function( ip ) {
				return dynamicIpsGadget.isDynamic( ip );
			}
		}

		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 += ' ' + userClass.join( ' ' );
			}
		}

		this.cache = {};
	}
};

coloredNicknamesGadget.init();