Jump to content

User:InvalidOS/ShowComments.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
importStylesheet('User:InvalidOS/ShowComments.css');

function filterNone()
{
    return NodeFilter.FILTER_ACCEPT;
}

// comment node NodeType is 8

function getCommentInfo()
{
    var comments = [[]];
    // Fourth argument, which is actually obsolete according to the DOM4 standard, is required in IE 11
    // Thanks stack overflow
    // var iterator = document.createNodeIterator(document.getElementById("mw-content-text"), NodeFilter.SHOW_COMMENT, filterNone, false);
    var el = document.getElementById("mw-content-text");
    // var curNode;
    /*
    while (curNode = iterator.nextNode())
    {
        var arrayToPush = [];
        arrayToPush[0] = curNode;
        arrayToPush[1] = curNode.textContent;
        comments.push(arrayToPush);
    }
    */
    
    var node;
    var push;
    
    // thanks to stack overflow for helping me find this
    
    for (var i = 0; i < el.childNodes.length; i++)
    {
        node = el.childNodes[i];
        push = [];
        if (node.nodeType === 8)
        {
            console.log("Node " + i.toString() +  "is a comment.");
            console.log("Node value: " + node.nodeValue.toString());
			pushArr = [node, node.nodeValue.toString()];
            comments.push(pushArr);
        }
        else
        {
            console.log("Node " + i.toString() +  "not a comment.");
        }
    }
    
    return comments;
}

$(document).ready(function()
{
	var commentInfo = getCommentInfo();
	for (x = 0; x < commentInfo.length; x++)
	{
		commentInfo[x][0].OuterHTML = '<span class="commentDisplay">&lt;!--' + commentInfo[x][1] + '--&rt;</span>';
	}
});

/*$(document).ready(renderComments);*/