User:Gary/comment highlighter.coffee

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).

FIXME: Remove dependency on User:Gary/comments in local time.js. FIXME: Fix on WP:RFA nominations (!votes, as in comments wrapped in   s). class CommentHighlighter now = new Date

@calculateColorRatio: (maxTime, minPercentage, maxPercentage, timestamp) -> minPercentage = minPercentage / 100 maxPercentage = maxPercentage / 100 colorRatio = ((maxPercentage - minPercentage) *     ((now.valueOf - timestamp.valueOf) / maxTime) +      minPercentage) * 100

if colorRatio < minPercentage colorRatio = minPercentage

colorRatio

@getCommentParent: (node, commentDivsExist) -> possibleParentNodes = ['DD', 'DIV', 'LI', 'P'] parent = node.parent

if parent.closest('.diff').length return $ else if commentDivsExist and $.inArray(parent[0].nodeName,     possibleParentNodes) > -1 return parent

possibleParents = [] possibleResults = {}

for possibleParentNode in possibleParentNodes 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((a, b) -> a[1] - b[1])

if possibleParents[0][1] parent = possibleParents[0][0]

if parent.length and not commentDivsExist and parent.contents.length newParent = $(' ') parent.contents.each (index, element) -> node = $(element) return false if node[0].nodeName is 'DL' newParent.append(node) parent.prepend(newParent) return newParent else return parent

@highlightComment: (timestamp, maxTime) -> timestampValue = parseInt(timestamp.attr('timestamp')) parent = @getCommentParent(timestamp, false)

unless parent.length return true

parent.attr('title', timestamp.text)

if now.valueOf - timestampValue < maxTime colorRatio = @calculateColorRatio(maxTime, 50, 100,       new Date(timestampValue)) parent.css('background-color', 'rgb(100%, 100%, ' + colorRatio + '%)')

@highlightUsername: (   link, formattedUsername, secondUsername, usernameBackground) ->

linkIsGood = link and link.attr('href') usernameCheck = @linkLinksToUsername(formattedUsername, link)

if secondUsername secondUsernameCheck = @linkLinksToUsername(secondUsername, link) else secondUsernameCheck = false

if linkIsGood and (usernameCheck or secondUsernameCheck) and not link.closest('#contentSub').length parent = @getCommentParent(link, true) if not parent.length return true parent.css('background-color', usernameBackground) link.parentsUntil('.mw-content-ltr').last.prevUntil('h2') .last.prev.css('background-color', usernameBackground)

@init: => # 24 hours before we don't color a comment anymore maxTime = 1000 * 60 * 60 * 24

# Highlight messages posted today (REQUIRES WP:Comments in Local Time   # SCRIPT) $('span.localcomments').each (index, element) => @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 (index, element) -> childComment = $(element) parentComment = childComment.parent.closest('.comment') 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? formattedUsername = 'User:' + myUsername.replace(/\ /g, '_') usernameBackground = '#eef'

if myUsername is 'Gary' secondUsername = 'User:Gary_King'

$('#bodyContent a').each (index, element) => @highlightUsername($(element), formattedUsername,         secondUsername, usernameBackground)

@linkLinksToUsername: (username, link) -> if link.attr('href') and link.attr('href').indexOf(username) > -1 and link.attr('href').indexOf(username) is     (link.attr('href').length - username.length) true else false

runCHScript = -> if window.mw.config.get('wgAction') is 'view' and isDiscussionPage? and isDiscussionPage setTimeout -> CommentHighlighter.init , 0
 * 1) This script depends on "Comments in Local Time", so it should run after that.

$ -> runCHScript