User:Gary/comment highlighter.coffee
Appearance
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
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
- This script depends on "Comments in Local Time", so it should run after that.
runCHScript = ->
if window.mw.config.get('wgAction') is 'view' and isDiscussionPage? and isDiscussionPage setTimeout -> CommentHighlighter.init() , 0
$ ->
runCHScript()