User:Eviolite/UHSc.js

Source: Wikipedia, the free encyclopedia.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Script credit: Novem Linguae, "Forked from a script with many authors: @Chlod, Pythoncoder, Bellezzasolo, Theopolisme, Amalthea, and Ais523."
// From [[User:Novem Linguae/Scripts/UserHighlighterSimple]].
// Slightly modified by me to work like markAdmins and disabling colors
//<nowiki>

mw.hook('wikipage.content').add(function($content){
	$.when(
		// TODO: FIX CACHING. This is broken. Need to switch from index.php to api.php
		// &maxage=86400&smaxage=86400&uselang=content = cache for 1 day
		
		// staff and sysadmins (updated manually when I get around to it)
		/* $.getJSON(mw.config.get('wgScriptPath')+'/index.php?action=raw&ctype=application/json&maxage=86400&smaxage=86400&uselang=content&title=User:Novem_Linguae/User_lists/Staff_and_sysadmins.js', function(data){
			staffdata = data;
		}), */
		
		// productive IP's (updated manually when I get around to it)
		// these are some IP's that I've selected that have hundreds of edits and are a net positive to the encyclopedia. We'll highlight them gray (extended confirmed).
		$.getJSON(mw.config.get('wgScriptPath')+'/index.php?action=raw&ctype=application/json&maxage=86400&smaxage=86400&uselang=content&title=User:Novem_Linguae/User_lists/Productive_IPs.js', function(data){
			productiveIPs = data;
		}),
		
		// arbcom (updated manually by various admins)
		$.getJSON(mw.config.get('wgScriptPath')+'/index.php?action=raw&ctype=application/json&maxage=86400&smaxage=86400&uselang=content&title=User:AmoryBot/crathighlighter.js/arbcom.json', function(data){
			acdata = data;
		}),
		
		// everybody else (updated daily by my bot)
		$.getJSON(mw.config.get('wgScriptPath')+'/index.php?action=raw&ctype=application/json&maxage=86400&smaxage=86400&uselang=content&title=User:NovemBot/userlist.js', function(data){
			swdata = data['steward'];
			crdata = data['bureaucrat'];
			addata = data['sysop'];
		//	nprdata = data['patroller'];
			ecdata = data['extendedconfirmed'];
			tenkdata = data['10k'];
		}),
		
		// functionaries
		$.getJSON(mw.config.get('wgScriptPath')+'/index.php?action=raw&ctype=application/json&title=User:AmoryBot/crathighlighter.js/oversight.json', function(data){
			osdata = data;
		}),
		$.getJSON(mw.config.get('wgScriptPath')+'/index.php?action=raw&ctype=application/json&title=User:AmoryBot/crathighlighter.js/checkuser.json', function(data){
			cudata = data;
		})

	).then(function() {
		// Combine some vars that we couldn't combine above due to async
		swdata = {
			...{'Jimbo Wales': 1},
			// ...staffdata,
			...swdata
		};
		ecdata = {
			...ecdata,
			...productiveIPs
		};
		
		ADMINHIGHLIGHT_NAMESPACES = [-1,2,3];
		mw.loader.using(['mediawiki.util','mediawiki.Uri', 'mediawiki.Title'], function() {
			// Note: .plainlinks is for Wikipedia Signpost articles
			
			//mw.util.addCSS(".override-signature-colors, .override-signature-colors span, .plainlinks .override-signature-colors.external, .override-signature-colors b, .override-signature-colors font {color: rgb(102, 177, 250) !important; background-color: transparent !important;}");
			
			// extended confirmed (53,000)
			//mw.util.addCSS(".userhighlighter_excon, .userhighlighter_excon span, .plainlinks .userhighlighter_excon.external, .userhighlighter_excon b, .userhighlighter_excon font {background-color: lightgray !important;}"); // darkgray
			
			// 10k edits (10,000)
			//mw.util.addCSS(".userhighlighter_tenk, .userhighlighter_tenk span, .plainlinks .userhighlighter_tenk.external, .userhighlighter_tenk b, .userhighlighter_tenk font {background-color: #9c9 !important;}"); // light brown: #d2691e
			
			// New page reviewer (714)
			//mw.util.addCSS(".userhighlighter_npruser, .userhighlighter_npruser span, .plainlinks .userhighlighter_npruser.external, .userhighlighter_npruser b, .userhighlighter_npruser font {background-color: #99f !important;}");
			
			// Admin
			//mw.util.addCSS(".userhighlighter_sysop, .userhighlighter_sysop span, .plainlinks .userhighlighter_sysop.external, .userhighlighter_sysop b, .userhighlighter_sysop font {background-color: #9ff !important;}");
			
			// bureaucrat
			//mw.util.addCSS(".userhighlighter_bureaucrat, .userhighlighter_bureaucrat span, .plainlinks .userhighlighter_bureaucrat.external, .userhighlighter_bureaucrat b, .userhighlighter_bureaucrat font {background-color: orange !important; color: #0645ad !important; }");
			
			// arbcom
			//mw.util.addCSS(".userhighlighter_arbcom, .userhighlighter_arbcom span, .plainlinks .userhighlighter_arbcom.external, .userhighlighter_arbcom b, .userhighlighter_arbcom font {background-color: #CC3F3F !important; color: white !important;}");
			
			// steward
			//mw.util.addCSS(".userhighlighter_steward, .userhighlighter_steward span, .plainlinks .userhighlighter_steward.external, .userhighlighter_steward b, .userhighlighter_steward font {background-color: hotpink !important; color: #0645ad !important; }");
			
			var lastUser = ""; // don't mark the same user twice in a row
			$('#article a, #bodyContent a, #mw_contentholder a').each(function(index,linkraw){
				try {
					var link = $(linkraw);
					var url = link.attr('href');
					
					// Skip <a> elements that aren't actually links; skip anchors
					if ( ! url || url.charAt(0) === '#' ) {lastUser = ""; return;} 
					
					//require http(s) links, avoid "javascript:..." etc. which mw.Uri does not support
					if (
						url.lastIndexOf("http://", 0) !== 0 &&
						url.lastIndexOf("https://", 0) !== 0 &&
						url.lastIndexOf("/", 0) !== 0
					) {
						lastUser = "";
						return;
					}
					
					var uri = new mw.Uri(url);
					
					// Skip links with query strings
					// Example: The pagination links, diff links, and revision links on the Special:Contributions page
					// Those all have "query strings" such as "&oldid=1003511328"
					// Exception: Users without a user page (red link) need to be highlighted
					let isRedLinkUserPage = url.startsWith('/w/index.php?title=User:') && url.endsWith('&action=edit&redlink=1');
					if ( ! $.isEmptyObject(uri.query) && ! isRedLinkUserPage ) {lastUser = ""; return;}
					
					if ( uri.host == 'en.wikipedia.org' ) {
						// Figure out the wikipedia article title of the link
						let titleParameterOfURL = mw.util.getParamValue('title',url); // for links in the format /w/index.php?title=Blah
						let URI = decodeURIComponent(uri.path.slice(6)); // for links in the format /wiki/PageName. Slice off the /wiki/ (first 6 characters)
						let title = titleParameterOfURL || URI;
						var mwtitle = new mw.Title(title);
						
						if ( $.inArray(mwtitle.getNamespaceId(), ADMINHIGHLIGHT_NAMESPACES) >= 0 ) {
							var user = mwtitle.getMain().replace(/_/g," ");
							
							if (mwtitle.getNamespaceId() === -1) {
								user = user.replace('Contributions/',''); // For special page "Contributions/<username>"
								user = user.replace('Contribs/',''); // The Contribs abbreviation too
							}
							
							let hasAdvancedPermissions = false;
							let label = ")";
							
							if (swdata[user] == 1) {
							//	link.addClass(link.attr('class') + ' userhighlighter_steward');
							//	if (link.attr("title") == null || link.attr("title").startsWith("User:")) link.attr("title", "Steward");
							//	hasAdvancedPermissions = true;
								label = "/S" + label;
							}
							if (osdata[user] == 1){
								label = "/OS" + label;
							}
							if (cudata[user] == 1){
								label = "/CU" + label;
							}
							if(crdata[user] == 1) {
							//	link.addClass(link.attr('class') + ' userhighlighter_bureaucrat');
							//	if (link.attr("title") == null || link.attr("title").startsWith("User:")) link.attr("title", "Bureaucrat");
							//	hasAdvancedPermissions = true;
								label = "/B" + label;
							}
							if(acdata[user] == 1) {
							//	link.addClass(link.attr('class') + ' userhighlighter_arbcom');
							//	if (link.attr("title") == null || link.attr("title").startsWith("User:")) link.attr("title", "Arbitration Committee member");
							//	hasAdvancedPermissions = true;
								label = "/ARB" + label;
							}
							if (addata[user] == 1) {
							//	link.addClass(link.attr('class') + ' userhighlighter_sysop');
							//	if (link.attr("title") == null || link.attr("title").startsWith("User:")) link.attr("title", "Admin");
							//	hasAdvancedPermissions = true;
								label = "/A" + label;
							}
							//if(nprdata[user] == 1) {
							//	link.addClass(link.attr('class') + " userhighlighter_npruser");
							//	if (link.attr("title") == null || link.attr("title").startsWith("User:")) link.attr("title", "New page reviewer");
							//	hasAdvancedPermissions = true;
							//}
							if(ecdata[user] == 1) {
							//	link.addClass(link.attr('class') + " userhighlighter_excon");
								if(link.attr("title").indexOf("(Extended confirmed)") == -1) {link.attr("title", link.attr("title") + " (Extended confirmed)");}
							//	hasAdvancedPermissions = true;
							}
							if(tenkdata[user] == 1) {
							//	link.addClass(link.attr('class') + " userhighlighter_tenk");
								if(link.attr("title").indexOf("(Extended confirmed)") == -1 && link.attr("title").indexOf("(More than 10,000 edits)") == -1) {link.attr("title", link.attr("title") + " (More than 10,000 edits)");}
								if(link.attr("title").indexOf("(Extended confirmed)") != -1 && link.attr("title").indexOf("(More than 10,000 edits)") == -1)  {link.attr("title", link.attr("title").replace(/\(Extended confirmed\)/,"(More than 10,000 edits)"));}
							//	hasAdvancedPermissions = true;
							}
							// If the user has any advanced perms, they are likely to have a signature, so be aggressive about overriding the background and foreground color. That way there's no risk their signature is unreadable due to background color and foreground color being too similar. Don't do this for users without advanced perms... being able to see a redlinked username is useful.
							//if ( hasAdvancedPermissions ) {
							//	link.addClass(link.attr('class') + ' override-signature-colors');
							//}
							// labels
							if(productiveIPs[user] == 1 && lastUser != user) { //deal with trusted IPs separately
								link.addClass(link.attr('class') + " userhighlighter_excon");
								link.attr("title", "Trusted IP");
								if(linkraw.innerHTML.indexOf("userLabel") == -1){linkraw.innerHTML += `<sup class="userLabel">(trusted IP)</sup>`;}
							} else {
								label = label.replace("/","\u00A0(");
								if(lastUser == user){label = ")";}
								// this is probably a very bad idea
								if(label != ")" && linkraw.innerHTML.indexOf("userLabel") == -1){linkraw.innerHTML += `<b class="userLabel">${label}</b>`;}
							}
							lastUser = user; 
						}
					}
				} catch (e) {
					// Sometimes we will run into unparsable links. No big deal. Just move on.
				}
			});
		});
	});
});

//</nowiki>