User:PleaseStand/highlight-comments-dev.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:PleaseStand/highlight-comments-dev. |
/**
* Highlights specific users' posts to discussion pages using a CSS class.
*
* Originally written by PleaseStand in 2010, updated for MediaWiki 1.17 in 2011
* Released to the public domain; see http://en.wikipedia.org/wiki/Template:PD-self
*/
// Work around mw.Map brokenness
if ( !mw.Map ) {
mw.Map = mw.config.constructor;
}
// Add mw.user.name() if it does not exist
if ( !mw.user.name ) {
mw.user.name = function() {
return mw.config.get( 'wgUserName' );
};
}
window.highlightComments = new ( function( $ ) {
var linkMap = new mw.Map(),
classNumber = 0;
/**
* Give comments linking to any given page a specific CSS class.
* Essentially, we need to find the comment's container and wrap (except where unnecessary)
* everything inside except replies to that comment. We can filter the replies out in that
* they are inside other element types that have the effect of indenting the text.
* @param linkMap A mw.Map from names of linked pages to CSS class names
*/
function wrapComments( linkMap ) {
// Elements containing comments or indented text (replies to those comments)
var commentTags = 'dd, li, p',
indentTags = 'dl, ol, ul';
$( 'a' ).each( function() {
if ( linkMap.exists( this.title ) ) {
var className = linkMap.get( this.title );
$(this).closest( commentTags ).contents().not( indentTags ).each( function() {
if ( this.nodeType == 1 ) {
$( this ).addClass( className );
} else {
$( this ).wrap( $( '<span/>', { 'class': className } ) );
}
} );
}
} );
}
/**
* Add a group of users whose comments should be given the same CSS class.
* @param className The CSS class name to use
* @param users An array of usernames
*/
this.addClass = function( className, users ) {
var ns = mw.config.get( 'wgFormattedNamespaces' );
for ( var i = 0; i < users.length; ++i ) {
var userName = users[i],
userPage = ns[2] + ':' + userName,
userTalkPage = ns[3] + ':' + userName;
linkMap.set( userPage, className );
linkMap.set( userTalkPage, className );
}
};
/**
* Add a group of users whose comments should be highlighted in the same color.
* @param color The CSS background-color to use
* @param users An array of usernames
* @return The resulting CSSStyleSheet object
*/
this.addColor = function( color, users ) {
var className = 'highlighted-comment-' + classNumber++;
this.addClass( className, users );
return mw.util.addCSS( '.' + className + ' { background-color: ' + color + '; }' );
};
this.addColor( '#ff7', [ mw.user.name() ] );
$( function() {
wrapComments( linkMap );
} );
} )( $ );