User:Gary/comment highlighter.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Gary/comment highlighter. |
/*
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;
}
function commentHighlighter()
{
// 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 (parent.parentsUntil('.diff').parent().hasClass('diff')) return $();
else 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.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.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('#bodyContent').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);
if (!isDiscussionPage) $.get(mw.config.get('wgScript') + '?title=User:Gary_King/discussion_pages.js&action=raw', checkIfDiscussionPage);
else commentHighlighter();
});