Jump to content

User:DatRoot/Extensions.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by DatRoot (talk | contribs) at 00:31, 11 November 2007 (Added basic history filter). 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.
String.isNullOrEmpty = function(s) 
{
    return !s || s.length == 0;
}
String.prototype.startsWith = function(s, ignoreCase)
{
    if(ignoreCase) return this.substr(0, s.length).toLowerCase() == s.toLowerCase();
    else return this.substr(0, s.length) == s;
}

function getChildByClassName(parent, name)
{
    var elem = parent.firstChild;
    while(elem != null && elem.className != name) elem = elem.nextSibling;
    return elem;
}

function getChildByNodeName(parent, name)
{
    var elem = parent.firstChild;
    while(elem != null && elem.nodeName != name) elem = elem.nextSibling;
    return elem;
}

function getQueryParams()
{
    var params = [];
    var queryString = location.search;
    if(queryString.length > 0)
    {
        var paramStrings = queryString.substr(1).split("&");
        for(var i = 0; i < paramStrings.length; i++)
        {
            var nameValuePair = unescape(paramStrings[i]).split("=");
            params[nameValuePair[0]] = nameValuePair[1] || "";
        }
    }
    
    return params;
}

function HistoryPageExtensions() {

    function setup() 
    {
        var params = getQueryParams();
        var sectionName = params["section"] || "";
        
        // Create filter link element that will be cloned and added every list item that 
        // specifies a section
        var filterElem = document.createElement("a");
        filterElem.href = wgServer + wgScript + "?title=" + wgPageName + "&action=history&section=";
        filterElem.innerHTML = "F";
        
        //debugger;
        for(var itemElem = document.getElementById("pagehistory").firstChild;
            itemElem != null; itemElem = itemElem.nextSibling)
        {
            if(itemElem.nodeName != "LI") continue;
            var itemSectionName = "";
            
            // Look for section name in comment and add filter link
            var commentElem = getChildByClassName(itemElem, "comment");
            if(commentElem) commentElem = getChildByClassName(commentElem, "autocomment");
            if(commentElem)
            {
                itemSectionName = commentElem.lastChild.nodeValue;
                var itemFilterElem = filterElem.cloneNode(true);
                itemFilterElem.href += escape(itemSectionName);
                commentElem.insertBefore(itemFilterElem, commentElem.firstChild);
            }
            
            // Hide list item if its section doesn't match the filter
            if(!itemSectionName.startsWith(sectionName, true)) itemElem.style.display = none;
        }
    }
    
    if(wgAction == "history") addOnloadHook(setup);
}

HistoryPageExtensions();