Jump to content

User:Nihonjoe/userhighlighter.js

From 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.
// Adapted from https://en.wikipedia.org/w/index.php?title=User:Amorymeltzer/crathighlighter.js&oldid=1043708073
//
// This only highlights users on this list: 
//    https://en.wikipedia.org/w/index.php?title=User:Nihonjoe/userhighlighter.js/userlist.json
// Features added:
//
// If you want different colors, add something like
// .userhighlighter {background-color: red !important}
// to your common.css file.
//
// <nowiki>


(function($) {
var highlight_order = window.highlight_order || ['editor'];
var all_groups = window.all_groups || false;
if (all_groups) {
	highlight_order.reverse();
}

// Core function for applying classes to the parsed JSON data
var main = function(data) {
	var USERHIGHLIGHT_EXTLINKS = window.USERHIGHLIGHT_EXTLINKS || false;
	var USERHIGHLIGHT_NAMESPACES = [-1, 2, 3];
	var classDefs = {
		'editor': '888888'
	};

	mw.loader.using(['mediawiki.util', 'mediawiki.Uri', 'mediawiki.Title'], function() {
		for (var perm in highlight_order) {
			mw.util.addCSS('.userhighlighter_' + highlight_order[perm] + ' {background-color: #' + classDefs[highlight_order[perm]] + '}');
		}

		$('#mw-content-text a').each(function(index, linkraw) {
			try {
				var link = $(linkraw);
				var url = link.attr('href');
				if (!url || url === '/wiki/' || url.charAt(0) === '#') {
					return;
				} // Skip <a> elements that aren't actually links; skip anchors
				if (url.lastIndexOf('http://', 0) !== 0 && url.lastIndexOf('https://', 0) !== 0 && url.lastIndexOf('/', 0) !== 0) {
					return;
				} // require http(s) links, avoid "javascript:..." etc. which mw.Uri does not support
				if (link[0].parentElement.className && link[0].parentElement.classList[0] === 'autocomment') {
					return;
				} // Skip span.autocomment links aka automatic section links in edit summaries
				if (link[0].tagName === 'IMG') {
					return;
				} // Don't highlight image links
				if (link[0].className && link[0].classList[0] === 'external') {
					return;
				} // Avoid errors on hard-to-parse external links
				url = url.replace(/%(?![0-9a-fA-F][0-9a-fA-F])/g, '%25');
				var uri = new mw.Uri(url);
				if (!USERHIGHLIGHT_EXTLINKS && !$.isEmptyObject(uri.query)) {
					return;
				} // Skip links with query strings if highlighting external links is disabled
				if (uri.host === 'en.wikipedia.org') {
					var mwtitle = new mw.Title(mw.util.getParamValue('title', url) || decodeURIComponent(uri.path.slice(6))); // Try to get the title parameter of URL; if not available, remove '/wiki/' and use that
					if ($.inArray(mwtitle.getNamespaceId(), USERHIGHLIGHT_NAMESPACES) >= 0) {
						var user = mwtitle.getMain().replace(/_/g, ' ');
						if (mwtitle.getNamespaceId() === -1) {
							user = user.replace('Contributions/', '');
						}
						$.each(highlight_order, function(_ix, ug) {
							if (data[ug][user] === 1) {
								link.addClass('userhighlighter_' + ug);
								return all_groups; // Exit on first match if false, continue if true
							}
						});
					}
				}
			} catch (e) {
				// Sometimes we will run into unparsable links, so just log these and move on
				mw.log.warn('userhighlighter.js unparsable link', e.message, linkraw);
			}
		});
	});
};

// Grab or generate the user data then actually run the main function
var userhighlighterdata;
try {
	userhighlighterdata = JSON.parse(localStorage.getItem('userhighlighterjson'));
} catch (e) {
	mw.log.error('userhighlighter: failed to parse local storage', e.message);
}
var cache_len = window.cache_hours || 1;
cache_len *= 60 * 60 * 1000; // milliseconds
if (!userhighlighterdata || !userhighlighterdata.date || (Date.now() - new Date(userhighlighterdata.date).getTime()) > cache_len) {
	userhighlighterdata = {};
	var promises = [];
	var baseUrl = '/w/index.php?action=raw&ctype=application/json&title=User:Nihonjoe/userhighlighter.js/';
	$.each(highlight_order, function(idx, perm) {
		var url = baseUrl + perm + '.json';
		var deferred = $.getJSON(url, function(data) {
			userhighlighterdata[perm] = data;
		});
		promises.push(deferred);
	});

	$.when.apply(null, promises).then(function() {
		userhighlighterdata.date = Date.now();
		localStorage.setItem('userhighlighterjson', JSON.stringify(userhighlighterdata));
		main(userhighlighterdata);
	});
} else {
	main(userhighlighterdata);
}
})(jQuery);
// </nowiki>