Jump to content

User:PleaseStand/highlight-comments.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by PleaseStand (talk | contribs) at 17:51, 11 April 2010 (debug). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<pre><nowiki>
 
// highlight-comments.js - written by PleaseStand, 2010.
// Source: http://en.wikipedia.org/wiki/User:PleaseStand/highlight-comments.js
// NOTE: This script is in the public domain (not copyrighted). For details, see
// http://en.wikipedia.org/wiki/Template:PD-self
 
// DESCRIPTION: Highlights your posts to discussion pages
// (at least a significant part). This is just the JS part; you need a
// corresponding CSS line to actually affect text color. An example:
 
// .mycomment { background: #ff9; }
 
/*global window, document, jQuery, wgFormattedNamespaces, wgUserName,
HighlightCommentsJsDescription, importScriptURI, hookEvent*/

// Function names
var HighlightCommentsJsHighlight, HighlightCommentsJsUnhighlight;

// Global variables
var HighlightCommentsJsWrapped = false;

HighlightCommentsJsHighlight = function(){
    // Lists the tags that can contain comments/indented text respectively.
    var commentTags = "dd,li,p";
    var indentTags = "dl,ol,ul";
    
    if(!HighlightCommentsJsWrapped) {
        // Do the work, adding span wrappers
        jQuery("a[title=\"" + (wgFormattedNamespaces[2] + ":" +
            wgUserName).replace(/"/g, "\\\"") + "\"]").closest(commentTags).
            contents().not(indentTags).
            wrap(jQuery("<span class=\"mycomment\"/>"));
        // Set the flag that wrapping is done
        HighlightCommentsJsWrapped = true;
    } else {
        // Just change the classes
        jQuery(".mycomment-off").removeClass("mycomment-off").
            addClass("mycomment");
    }
};

HighlightCommentsJsUnhighlight = function(){
    // Change the classes
    jQuery(".mycomment").removeClass("mycomment").addClass("mycomment-off");
};

function HighlightCommentsJsRequireJquery(func, tries) {
    // Monobook skin, unlike the newer Vector skin, doesn't load jQuery :(
    if(typeof jQuery == "undefined" && tries) {
        if(wgUserName == "PleaseStand") {
            window.alert(tries);
        }
        // Importing won't actually happen more than once
        importScriptURI("http://bits.wikimedia.org/skins-1.5/common/jquery.min.js");
        return window.setTimeout(HighlightCommentsJsRequireJquery, 250,
            func, tries - 1);
    }
    if(typeof jQuery == "function") {
        if(wgUserName == "PleaseStand") {
            window.alert("Found jQuery!");
        }
        return func();
    }
    return false;
}

hookEvent("load", function(){
    HighlightCommentsJsRequireJquery(function(){
        var desc, button, buttonLink;
        
        if(typeof HighlightCommentsJsDescription == "undefined") {
            // Default toolbox link text
            desc = "Toggle comment highlighting";
        } else {
            // User-defined toolbox link text
            desc = HighlightCommentsJsDescription;
        }
        
        // Create and add the toolbox link
        button = jQuery("<li id=\"n-highlightcomments\"/>").
            append(buttonLink = jQuery("<a/>").html(desc));
        jQuery("#p-tb ul").append(button);
        buttonLink.toggle(HighlightCommentsJsHighlight,
            HighlightCommentsJsUnhighlight);
    }, 10);
});
//</nowiki></pre>