User:Gary/comment highlighter.js: Difference between revisions

Content deleted Content added
rename
fix
 
(13 intermediate revisions by the same user not shown)
Line 1:
// Generated by CoffeeScript 2.1.1
/*
(function() {
COMMENT HIGHLIGHTER
/*
Description: Highlights recent comments yellow, while your own comments are highlighted in blue. (Requires [[User:Gary/comments in local time.js]] for now).
COMMENT HIGHLIGHTER
Description: Highlights recent comments yellow, while your own comments are
FIXME Remove dependency on [[User:Gary/comments in local time.js]].
highlighted in blue.
FIXME Fix on [[WP:RFA]] nominations (!votes, as in comments wrapped in <li>s).
*/
 
(Requires [[User:Gary/comments in local time.js]] for now).
if (typeof(unsafeWindow) != 'undefined')
{
addOnloadHook = unsafeWindow.addOnloadHook;
mw = unsafeWindow.mw;
}
 
FIXME: Remove dependency on [[User:Gary/comments in local time.js]].
function commentHighlighter()
FIXME: Fix on [[WP:RFA]] nominations (!votes, as in comments wrapped in
{
<li>s).
// Get the closest parent node for a comment.
*/
function sortParents(a, b)
var CommentHighlighter, runCHScript;
{
return a[1] - b[1];
}
 
CommentHighlighter = (function() {
function getCommentParent(node, commentDivsExist)
var now;
{
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;
 
class CommentHighlighter {
var possibleParents = [];
static calculateColorRatio(maxTime, minPercentage, maxPercentage, timestamp) {
for (var i = 0; i < possibleParentNodes.length; i++)
var colorRatio;
{
minPercentage = minPercentage / 100;
var possible = possibleParentNodes[i].toLowerCase();
maxPercentage = maxPercentage / 100;
eval('var ' + possible + ' = node.parentsUntil(\'' + possible + '\');');
colorRatio = ((maxPercentage - minPercentage) * ((now.valueOf() - timestamp.valueOf()) / maxTime) + minPercentage) * 100;
possibleParents.push([eval(possible).eq(0).parent(), eval(possible).length]);
if (colorRatio < minPercentage) {
}
colorRatio = minPercentage;
possibleParents.sort(sortParents);
}
if (possibleParents[0][1]) parent = possibleParents[0][0];
return colorRatio;
}
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);
});
 
static getCommentParent(node, commentDivsExist) {
parent.prepend(newParent);
var i, len, newParent, parent, possible, possibleParentNode, possibleParentNodes, possibleParents, possibleResults;
return newParent;
possibleParentNodes = ['DD', 'DIV', 'LI', 'P'];
}
else return parent = node.parent();
if (parent.closest('.diff').length) {
}
return $();
} else if (commentDivsExist && $.inArray(parent[0].nodeName, possibleParentNodes) > -1) {
return parent;
}
possibleParents = [];
possibleResults = {};
for (i = 0, len = possibleParentNodes.length; i < len; i++) {
possibleParentNode = possibleParentNodes[i];
possible = possibleParentNode.toLowerCase();
possibleResults[possible] = node.parentsUntil(possible);
possibleParents.push([possibleResults[possible].first().parent(), possibleResults[possible].length]);
}
// Get the closest parent node for a comment.
possibleParents.sort(function(a, b) {
return a[1] - b[1];
});
if (possibleParents[0][1]) {
parent = possibleParents[0][0];
}
if (parent.length && !commentDivsExist && parent.contents().length) {
newParent = $('<div class="comment"></div>');
parent.contents().each(function(index, element) {
node = $(element);
if (node[0].nodeName === 'DL') {
return false;
}
return newParent.append(node);
});
parent.prepend(newParent);
return newParent;
} else {
return parent;
}
}
 
static highlightComment(timestamp, maxTime) {
function calculateColorRatio(maxTime, minPercentage, maxPercentage, timestamp)
var colorRatio, parent, timestampValue;
{
timestampValue = parseInt(timestamp.attr('timestamp'));
var today = new Date();
parent = this.getCommentParent(timestamp, false);
minPercentage = minPercentage / 100;
if (!parent.length) {
maxPercentage = maxPercentage / 100;
return true;
var colorRatio = ((maxPercentage - minPercentage) * ((today.getTime() - timestamp.getTime()) / maxTime) + minPercentage) * 100;
}
if (colorRatio < minPercentage) colorRatio = minPercentage;
parent.attr('title', timestamp.text());
return colorRatio;
if (now.valueOf() - timestampValue < maxTime) {
}
colorRatio = this.calculateColorRatio(maxTime, 50, 100, new Date(timestampValue));
return parent.css('background-color', 'rgb(100%, 100%, ' + colorRatio + '%)');
}
}
 
static highlightUsername(link, formattedUsername, secondUsername, usernameBackground) {
var maxTime = 1000 * 60 * 60 * 24;
var linkIsGood, parent, secondUsernameCheck, usernameCheck;
linkIsGood = link && link.attr('href');
usernameCheck = this.linkLinksToUsername(formattedUsername, link);
if (secondUsername) {
secondUsernameCheck = this.linkLinksToUsername(secondUsername, link);
} else {
secondUsernameCheck = false;
}
if (linkIsGood && (usernameCheck || secondUsernameCheck) && !link.closest('#contentSub').length) {
parent = this.getCommentParent(link, true);
if (!parent.length) {
return true;
}
parent.css('background-color', usernameBackground);
return link.parentsUntil('.mw-content-ltr').last().prevUntil('h2').last().prev().css('background-color', usernameBackground);
}
}
 
static init() {
// Highlight messages posted today. (REQUIRES [[WP:COMMENTS IN LOCAL TIME]] SCRIPT)
var formattedUsername, maxTime, myUsername, secondUsername, usernameBackground;
$('span.localcomments').each(function()
// 24 hours before we don't color a comment anymore
{
maxTime = 1000 * 60 * 60 * 24;
var timestamp = $(this);
// Highlight messages posted today (REQUIRES [[WP:Comments in Local Time]]
var timestampValue = parseInt(timestamp.attr('timestamp'));
// SCRIPT)
var parent = getCommentParent(timestamp);
$('span.localcomments').each((index, element) => {
if (!parent.length) return true;
return CommentHighlighter.highlightComment($(element), maxTime);
parent.attr('title', timestamp.text());
});
// Check to see if any comments contain comments. None of them should. But if
// they do, then unwrap the parent .comment.
$('.comment').find('.comment').each(function(index, element) {
var childComment, parentComment;
childComment = $(element);
parentComment = childComment.parent().closest('.comment');
return parentComment.children().first().unwrap();
});
// Highlight discussion sections that I am linked from (i.e. that I
// participated or was mentioned in). Also highlight the line itself.
myUsername = window.mw.config.get('wgUserName');
if (myUsername != null) {
formattedUsername = 'User:' + myUsername.replace(/\ /g, '_');
usernameBackground = '#eef';
if (myUsername === 'Gary') {
secondUsername = 'User:Gary_King';
}
return $('#bodyContent a').each((index, element) => {
return CommentHighlighter.highlightUsername($(element), formattedUsername, secondUsername, usernameBackground);
});
}
}
 
static linkLinksToUsername(username, link) {
if ((new Date()).getTime() - timestampValue < maxTime)
if (link.attr('href') && link.attr('href').indexOf(username) > -1 && link.attr('href').indexOf(username) === (link.attr('href').length - username.length)) {
{
return true;
var colorRatio = calculateColorRatio(maxTime, 50, 100, new Date(timestampValue));
} else {
parent.css('background-color', 'rgb(100%, 100%, ' + colorRatio + '%)');
return false;
}
}
});
}
 
};
// 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';
 
now = new Date();
$('#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);
}
});
}
 
return CommentHighlighter;
function checkIfDiscussionPage(data)
{
eval(data);
if ($.inArray(mw.config.get('wgPageName').split('/')[0].replace(/_/g, ' '), discussionPages) != -1) commentHighlighter();
}
 
}).call(this);
// addOnloadHook required since this depends on Comments in Local Time.
 
addOnloadHook(function()
// This script depends on "Comments in Local Time", so it should run after that.
{
runCHScript = function() {
if (mw.config.get('wgAction') != 'view') return false;
if (window.mw.config.get('wgAction') === 'view' && (typeof isDiscussionPage !== "undefined" && isDiscussionPage !== null) && isDiscussionPage) {
return setTimeout(function() {
// check if this is a discussion page
return CommentHighlighter.init();
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;
}, 0);
if (!isDiscussionPage) $.get(mw.config.get('wgScript') + '?title=User:Gary_King/discussion_pages.js&action=raw', checkIfDiscussionPage);
}
else commentHighlighter();
});
 
$(function() {
return runCHScript();
});
 
}).call(this);