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

Content deleted Content added
some minor fixes
fix
 
(9 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).
function commentHighlighter()
{
// Get the closest parent node for a comment.
function sortParents(a, b)
{
return a[1] - b[1];
}
 
FIXME: Remove dependency on [[User:Gary/comments in local time.js]].
function getCommentParent(node, commentDivsExist)
FIXME: Fix on [[WP:RFA]] nominations (!votes, as in comments wrapped in
{
<li>s).
var possibleParentNodes = ['DD', 'DIV', 'LI', 'P'];
*/
var parent = node.parent();
var CommentHighlighter, runCHScript;
if (parent.parentsUntil('.diff').parent().hasClass('diff')) return $();
else if (commentDivsExist && $.inArray(parent[0].nodeName, possibleParentNodes) > -1) return parent;
 
CommentHighlighter = (function() {
var possibleParents = [];
var now;
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];
 
class CommentHighlighter {
if (parent.length && !commentDivsExist && parent.contents().length)
static calculateColorRatio(maxTime, minPercentage, maxPercentage, timestamp) {
{
var colorRatio;
var newParent = $('<div class="comment"></div>');
minPercentage = minPercentage / 100;
parent.contents().each(function()
maxPercentage = maxPercentage / 100;
{
colorRatio = ((maxPercentage - minPercentage) * ((now.valueOf() - timestamp.valueOf()) / maxTime) + minPercentage) * 100;
var node = $(this);
if (colorRatio < minPercentage) {
// if (node[0].nodeName == 'DL') return $();
colorRatio = minPercentage;
newParent.append(node);
}
});
return colorRatio;
parent.prepend(newParent);
}
return newParent;
}
else
{
return parent;
}
}
 
static getCommentParent(node, commentDivsExist) {
function calculateColorRatio(maxTime, minPercentage, maxPercentage, timestamp)
var i, len, newParent, parent, possible, possibleParentNode, possibleParentNodes, possibleParents, possibleResults;
{
possibleParentNodes = ['DD', 'DIV', 'LI', 'P'];
var today = new Date();
parent = node.parent();
minPercentage = minPercentage / 100;
if (parent.closest('.diff').length) {
maxPercentage = maxPercentage / 100;
return $();
var colorRatio = ((maxPercentage - minPercentage) * ((today.getTime() - timestamp.getTime()) / maxTime) + minPercentage) * 100;
} else if (commentDivsExist && $.inArray(parent[0].nodeName, possibleParentNodes) > -1) {
if (colorRatio < minPercentage) colorRatio = minPercentage;
return colorRatioparent;
}
}
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) {
var maxTime = 1000 * 60 * 60 * 24;
var colorRatio, parent, timestampValue;
timestampValue = parseInt(timestamp.attr('timestamp'));
parent = this.getCommentParent(timestamp, false);
if (!parent.length) {
return true;
}
parent.attr('title', timestamp.text());
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) {
// Highlight messages posted today. (REQUIRES [[WP:COMMENTS IN LOCAL TIME]] SCRIPT)
var linkIsGood, parent, secondUsernameCheck, usernameCheck;
$('span.localcomments').each(function()
linkIsGood = link && link.attr('href');
{
usernameCheck = this.linkLinksToUsername(formattedUsername, link);
var timestamp = $(this);
if (secondUsername) {
var timestampValue = parseInt(timestamp.attr('timestamp'));
secondUsernameCheck = this.linkLinksToUsername(secondUsername, link);
var parent = getCommentParent(timestamp, false);
} else {
if (!parent.length) return true;
secondUsernameCheck = false;
parent.attr('title', timestamp.text());
}
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() {
if ((new Date()).getTime() - timestampValue < maxTime)
var formattedUsername, maxTime, myUsername, secondUsername, usernameBackground;
{
// 24 hours before we don't color a comment anymore
var colorRatio = calculateColorRatio(maxTime, 50, 100, new Date(timestampValue));
maxTime = 1000 * 60 * 60 * 24;
parent.css('background-color', 'rgb(100%, 100%, ' + colorRatio + '%)');
// Highlight messages posted today (REQUIRES [[WP:Comments in Local Time]]
}
// SCRIPT)
});
$('span.localcomments').each((index, element) => {
return CommentHighlighter.highlightComment($(element), maxTime);
});
// 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) {
// Highlight discussion sections that I am linked from (i.e. that I participated or was mentioned in).
if (link.attr('href') && link.attr('href').indexOf(username) > -1 && link.attr('href').indexOf(username) === (link.attr('href').length - username.length)) {
// Also highlight the line itself.
return true;
var myUsername = mw.config.get('wgUserName');
} else {
var formattedUsername = 'User:' + myUsername.replace(/ /g, '_');
return false;
var usernameBackground = '#eef';
}
}
 
};
if (myUsername == 'Gary') var secondUsername = 'User:Gary_King';
else var secondUsername;
 
now = new Date();
function linkLinksToUsername(username, link)
{
if (link.attr('href').indexOf(username) > -1 && link.attr('href').indexOf(username) == (link.attr('href').length - username.length)) return true;
else return false;
}
 
return CommentHighlighter;
$('#bodyContent a').each(function()
{
var link = $(this);
var linkIsGood = link && link.attr('href');
var usernameCheck = linkLinksToUsername(formattedUsername, link);
if (secondUsername) var secondUsernameCheck = linkLinksToUsername(secondUsername, link);
else var secondUsernameCheck = false;
if (linkIsGood && (usernameCheck || secondUsernameCheck) && !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);
}
});
}
 
}).call(this);
function checkIfDiscussionPage(data)
{
eval(data);
if ($.inArray(mw.config.get('wgPageName').split('/')[0].replace(/_/g, ' '), discussionPages) > -1) commentHighlighter();
}
 
// addOnloadHookThis required since thisscript depends on "Comments in Local Time", so it should run after that.
addOnloadHook( runCHScript = function() {
if (window.mw.config.get('wgAction') === 'view' && (typeof isDiscussionPage !== "undefined" && isDiscussionPage !== null) && isDiscussionPage) {
{
return setTimeout(function() {
if (mw.config.get('wgAction') != 'view') return false;
return CommentHighlighter.init();
}, 0);
// 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/discussion_pages.js&action=raw', checkIfDiscussionPage);
 
else commentHighlighter();
$(function() {
});
return runCHScript();
});
 
}).call(this);