Jump to content

User:DatRoot/Extensions.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.
String.prototype.equals = function(other, ignoreCase)
{
    if(ignoreCase) return this.toLowerCase() == other.toLowerCase();
    else return this == other;
}
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;
}
String.prototype.trimEnd = function()
{
    for(var p = this.length - 1; p >= 0; p--)
    {
        var c = this.charAt(p);
        var matched = false;
        for(var i = 0; i < arguments.length; i++)
        {
            if(arguments[i] == c) 
            {
                matched = true;
                break;
            }
        }
        if(!matched) return this.substr(0, p + 1);
    }
    return "";
}

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 bodyContent = document.getElementById("bodyContent");
        var sections = {};
        
        // Get section name filter from address
        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 = mw.config.get('wgServer') + mw.config.get('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.trimEnd(" ", "-");
                sections[itemSectionName] = true;                
                
                var itemFilterElem = filterElem.cloneNode(true);
                itemFilterElem.href += itemSectionName;
                commentElem.insertBefore(itemFilterElem, commentElem.firstChild);
            }
            
            // Hide list item if its section doesn't match the filter
            if(sectionName.length > 0 && !itemSectionName.equals(sectionName, true)) itemElem.style.display = "none";
        }
        
        // Create combo box of section names
        var sectionSelect = document.createElement("select");
        for(var name in sections)
        {
            var opt = document.createElement("option");
            opt.value = name; opt.text = name;
            sectionSelect.add(opt, null);
            if(name == sectionName) opt.selected = true;
        }
        //addHandler(sectionSelect, "onchange", function() {
        //    alert(sectionSelect.value);
        //});
        bodyContent.insertBefore(sectionSelect, document.getElementById("histlegend"));
        bodyContent.insertBefore(document.createTextNode(" Section:"), sectionSelect);
        
        // Add the section name to other links on the page
        if(!String.isNullOrEmpty(sectionName))
        {
            for(var linkElem = document.getElementById("bodyContent").firstChild;
                linkElem != null; linkElem = linkElem.nextSibling)
            {
                if(linkElem.nodeName == "A") linkElem.href += "&section=" + sectionName;
            }
        }
    }
    
    if(wgAction == "history") addOnloadHook(setup);
}

HistoryPageExtensions();