Jump to content

User:Gary/comment highlighter.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Xeno (talk | contribs) at 22:24, 20 February 2014 (Xeno moved page User:Gary King/comment highlighter.js to User:Gary/comment highlighter.js without leaving a redirect: Automatically moved page while renaming the user "Gary King" to "Gary"). 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')
{
	addOnloadHook = unsafeWindow.addOnloadHook;
	mw = unsafeWindow.mw;
}

function commentHighlighter()
{
	// Get the closest parent node for a comment.
	function sortParents(a, b)
	{
		return a[1] - b[1];
	}

	function getCommentParent(node, commentDivsExist)
	{
		var possibleParentNodes = ['DD', 'DIV', 'LI', 'P'];
		
		var parent = node.parent();
		if (parent.parentsUntil('.diff').parent().hasClass('diff')) return $();
		else if (commentDivsExist && $.inArray(parent[0].nodeName, possibleParentNodes) != -1) return parent;

		var possibleParents = [];
		for (var i = 0; i < possibleParentNodes.length; i++)
		{
			var possible = possibleParentNodes[i].toLowerCase();
			eval('var ' + possible + ' = node.parentsUntil(\'' + possible + '\');');
			possibleParents.push([eval(possible).eq(0).parent(), eval(possible).length]);
		}
		possibleParents.sort(sortParents);
		if (possibleParents[0][1]) parent = possibleParents[0][0];
		
		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 $();
				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);
		if (!parent.length) return true;
		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 usernameBackground = '#eef';

	$('#bodyContent a').each(function()
	{
		var link = $(this);
		if (link && link.attr('href') && 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);
			if (!parent.length) return true;
			parent.css('background-color', usernameBackground);
			link.parentsUntil('.mw-content-ltr').last().prevUntil('h2').last().prev().css('background-color', usernameBackground);
		}
	});
}

function checkIfDiscussionPage(data)
{
	eval(data);
	if ($.inArray(mw.config.get('wgPageName').split('/')[0].replace(/_/g, ' '), discussionPages) != -1) commentHighlighter();
}

// addOnloadHook required since this depends on Comments in Local Time.
addOnloadHook(function()
{
	if (mw.config.get('wgAction') != 'view') return false;
	
	// check if this is a discussion page
	var isDiscussionPage = mw.config.get('wgCanonicalNamespace').indexOf('talk') != -1 || mw.config.get('wgCanonicalNamespace').indexOf('Talk') != -1 || ($('#ca-addsection').length ? true : false) || mw.config.get('wgPageName').indexOf('/Archive_') != -1;
	if (!isDiscussionPage) $.get(mw.config.get('wgScript') + '?title=User:Gary_King/discussion_pages.js&action=raw', checkIfDiscussionPage);
	else commentHighlighter();
});