User:Codeispoetry/markAdmins.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
 //<nowiki>

 //importScript('User:Codeispoetry/helperFunctions.js');

 function markAdmins()
 {
	if (!isSpecial && !isHistory && !isUser && !isTalk && !isProject && !isHelp && !isDiff) return;

	var req = newRequest();
	if (!req) return;
	req.open("GET", "/w/index.php?title=Special:Listusers&dir=prev&limit=500&group=sysop", false);
	req.send("");
	var admins = document.createElement('ul');
	admins.innerHTML = req.responseText.match(/<UL>(.+)<\/UL>/i);

	var allanchors = document.getElementsByTagName("A");      

	for ( i = 0 ; i < allanchors.length ; i++) {
		aNode = allanchors[i];
		// if it's a link to a user
		if ((href = aNode.getAttribute("href")) && (href.match(/(^http:\/\/commons\.|\/).+[Uu]ser(:|%3A)/) != null) && aNode.getAttribute("class") != "extiw") {
		
			var currUser = decodeURIComponent(href.match(/[Uu]ser(:|%3A)(.+)/)[2].replace(/_/, " "));

			li = admins.firstChild.firstChild;

			while ((li.firstChild.attributes.title.value < 'User:' + currUser) && (li = li.nextSibling)) {}

			if (li.firstChild.attributes.title.value == 'User:' + currUser) {
				var aMarkText = '';
				var lia = li.firstChild;
				while (lia = lia.nextSibling) {
					if (lia.attributes) {
						if (lia.attributes.title.value == 'Commons:Administrators')
							aMarkText += 'A' 
						else if (lia.attributes.title.value == 'Commons:Bureaucrats') 
							aMarkText += 'B/' 
						else if (lia.attributes.title.value == 'm:CheckUser_policy') 
							aMarkText += 'C/'; 
					}
				}
				var aMark = document.createElement("span");
				aMark.innerHTML = ' (' + aMarkText + ')';
				aNode.appendChild(aMark.cloneNode(true));
			}
		}
	}
 }

 function newRequest() {
	try {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else {
			return new ActiveXObject("MSXML2.XMLHTTP");
		}
	} catch (e) {
		return false;
	}
 }

 addOnloadHook(markAdmins);
 //</nowiki>