Jump to content

User:Gary/comment highlighter.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gary (talk | contribs) at 00:27, 14 April 2011 (f). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/*
	COMMENT HIGHLIGHTER
	Description: Highlights recent comments yellow, while your own comments are highlighted in blue. (Requires [[User:Gary King/comments in local time.js]] for now).
	
	FIXME Remove dependency on [[User:Gary King/comments in local time.js]].
	FIXME Fix on [[WP:RFA]] nominations (!votes, as in comments wrapped in <li>s).
*/

if (typeof(unsafeWindow) != 'undefined')
{
	var addOnloadHook = unsafeWindow.addOnloadHook;
	var mw = unsafeWindow.mw;
}

addOnloadHook(commentHighlighter);
function commentHighlighter()
{
	// Mirrored from [[User:Gary King/monobook.js]]
	var discussionPages = 
	[
		'Wikipedia:Articles for deletion',
		'Wikipedia:Featured article candidates',
		'Wikipedia:Featured article review',
		'Wikipedia:Featured list candidates',
		'Wikipedia:Featured list removal candidates',
		'Wikipedia:Featured picture candidates',
		'Wikipedia:Valued picture candidates',
		'Wikipedia:Requests for adminship'
	];

	var isDiscussionPage = ($.inArray(mw.config.get('wgPageName').split('/')[0].replace(/_/g, ' '), discussionPages) != -1 || mw.config.get('wgCanonicalNamespace').indexOf('talk') != -1 || mw.config.get('wgCanonicalNamespace').indexOf('Talk') != -1) || ($('#ca-addsection').length ? true : false);
	if (!isDiscussionPage || mw.config.get('wgAction') != 'view') return false;
	
	// Get the closest parent node for a comment.
	function sortParents(a, b)
	{
		return a[1] - b[1];
	}

	function getCommentParent(node, commentDivsExist)
	{
		var parent = node.parent();
		if (commentDivsExist && (parent[0].nodeName == 'DD' || parent[0].nodeName == 'P' || parent[0].nodeName == 'DIV')) return parent;

		var div = node.parentsUntil('div');
		var dd = node.parentsUntil('dd');
		var p = node.parentsUntil('p');

		var possibleParents = [[div.eq(0).parent(), div.length], [dd.eq(0).parent(), dd.length], [p.eq(0).parent(), p.length]];
		possibleParents.sort(sortParents);
		if (possibleParents[0][1]) parent = possibleParents[0][0];

		if (parent.parentsUntil('.diff').parent().hasClass('diff')) return $();
		else if (parent.length && !commentDivsExist && parent.contents().length)
		{
			var newParent = $('<div class="comment"></div>');
			parent.contents().each(function()
			{
				var node = $(this);
				if (node[0].nodeName == 'DL') return false;
				newParent.append(node);
			});

			parent.prepend(newParent);
			return newParent;
		}
		else return parent;
	}

	function calculateColorRatio(maxTime, minPercentage, maxPercentage, timestamp)
	{
		var today = new Date();
		minPercentage = minPercentage / 100;
		maxPercentage = maxPercentage / 100;
		var colorRatio = ((maxPercentage - minPercentage) * ((today.getTime() - timestamp.getTime()) / maxTime) + minPercentage) * 100;
		if (colorRatio < minPercentage) colorRatio = minPercentage;
		return colorRatio;
	}

	var maxTime = 1000 * 60 * 60 * 24;

	// Highlight messages posted today. (REQUIRES [[WP:COMMENTS IN LOCAL TIME]] SCRIPT)
	$('span.localcomments').each(function()
	{
		var timestamp = $(this);
		var timestampValue = parseInt(timestamp.attr('timestamp'));
		var parent = getCommentParent(timestamp);
		parent.attr('title', timestamp.text());

		if ((new Date()).getTime() - timestampValue < maxTime)
		{
			var colorRatio = calculateColorRatio(maxTime, 50, 100, new Date(timestampValue));
			parent.css('background-color', 'rgb(100%, 100%, ' + colorRatio + '%)');
		}
	});

	// Highlight discussion sections that I am linked from (i.e. that I participated or was mentioned in).
	// Also highlight the line itself.
	var formattedUsername = 'User:' + mw.config.get('wgUserName').replace(/ /g, '_');
	var blueBackground = '#eef';

	$('#bodyContent a').each(function()
	{
		var link = $(this);
		if (link.attr('href').indexOf(formattedUsername) != -1 && link.attr('href').indexOf(formattedUsername) == (link.attr('href').length - formattedUsername.length) && !link.parents('#contentSub').length)
		{
			var parent = getCommentParent(link, true);
			parent.css('background-color', blueBackground);
			link.parentsUntil('#bodyContent').last().prevUntil('h2').last().prev().css('background-color', blueBackground);
		}
	});
}