Jump to content

User:Ilmari Karonen/watchfilter.js

From Wikipedia, the free encyclopedia
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.
// FILTER WATCHLIST BY NAMESPACE <pre><nowiki>

// NOTE: This script is now mostly OBSOLETE, as similar functionality has been added to MediaWiki.  It also no longer works with "enhanced recent changes" enabled.

$(function () {
    if (unescape(window.location.href).indexOf("Special:Watchlist") < 0) return;

    // just one little ID attribute would be _so_ nice...
    var wlNotePara = document.getElementsByTagName('hr')[0];
    while (wlNotePara && !(wlNotePara.nodeType == 1 && wlNotePara.tagName.toLowerCase() == 'p'))
        wlNotePara = wlNotePara.nextSibling;
    if (!wlNotePara) return;

    var nameSpaces = new Array ('Talk', 'User', 'User talk', 'Wikipedia', 'Wikipedia talk',
                                'Image', 'Image talk', 'MediaWiki', 'MediaWiki talk',
                                'Template', 'Template talk', 'Help', 'Help talk',
                                'Category', 'Category talk', 'Portal', 'Portal talk');
    var list = new Array ();

    var prepareFilter = function () {
        var nsRegexp = new RegExp ("^(" + nameSpaces.join("|") + "):");
        var foundNameSpaces = new Array ();

        var dates = document.getElementById('content').getElementsByTagName('h4');
        for (var i = 0; i < dates.length; i++) {

            var node = dates[i].nextSibling;
            while (node && node.nodeType != 1) node = node.nextSibling;
            if (!node) continue;

            var sublist = new Array ();
            switch (node.tagName.toLowerCase()) {
              case 'ul':  // normal mode
                sublist = node.getElementsByTagName('li');
                break;

              case 'div': // enhanced mode
                var row = sublist[0] = document.createElement('span');
                node.insertBefore(row, node.firstChild);
                var subnode;
                while (subnode = row.nextSibling) {
                    row.appendChild(subnode);
                    if (subnode.nodeType == 1 && subnode.tagName.toLowerCase() == 'br') {
                        var nextRow = document.createElement('span');
                        node.insertBefore(nextRow, row.nextSibling);
                        row = sublist[sublist.length] = nextRow;
                    }
                }
                break;
            }
            for (var j = 0; j < sublist.length; j++) {
                var link = sublist[j].getElementsByTagName('a')[0];
                if (!link) continue;
                var nsPrefix = nsRegexp.exec(link.title);
                nsPrefix = (nsPrefix ? nsPrefix[1] : "(Main)");
                sublist[j].setAttribute('watchFilterNsPrefix', nsPrefix);
                foundNameSpaces[nsPrefix] = true;
                list[list.length] = sublist[j];
            }
        }
        for (var i = 0; i < nsSelect.options.length; i++) {
            if (nsSelect.options[i].value != "" && !foundNameSpaces[nsSelect.options[i].value]) {
                nsSelect.options[i].style.color = 'graytext';
                nsSelect.options[i].disabled = true;
            }
        }
    };

    var activateFilter = function () {
        var nsPrefix = nsSelect.options[nsSelect.selectedIndex].value;

        nsCheckbox.disabled = (nsPrefix == "");
        var invert = (nsPrefix != "" && nsCheckbox.checked);

        for (var i = 0; i < list.length; i++) {
            var show = (nsPrefix == "" || nsPrefix == list[i].getAttribute('watchFilterNsPrefix'));
            if (invert ? !show : show)
                list[i].className = list[i].className.replace(/(^|\s)hiddenStructure(\s|$)/, "$2");
            else
                list[i].className = list[i].className.replace(/(?:(^|\s)hiddenStructure(\s|$)|$)/, " hiddenStructure$2");
        }
    };

    var nsForm = document.createElement('form');
    nsForm.style.display = 'inline';
    nsForm.onsubmit = 'return false';
    nsForm.appendChild(document.createTextNode('Namespace: '));

    var nsSelect = document.createElement('select');
    nsSelect.name = 'ns';
    nsSelect.onchange = activateFilter;
    nsForm.appendChild(nsSelect);

    nsSelect.options[nsSelect.options.length] = new Option ("All", "", true);
    nsSelect.options[nsSelect.options.length] = new Option ("(Main)");
    for (var i = 0; i < nameSpaces.length; i++) {
        nsSelect.options[nsSelect.options.length] = new Option (nameSpaces[i]);
    }

    var nsCheckboxLabel = document.createElement('label');
    var nsCheckbox = document.createElement('input');
    nsCheckbox.type = 'checkbox';
    nsCheckbox.name = 'invert';
    nsCheckbox.value = '1';
    nsCheckbox.onclick = activateFilter;
    nsCheckboxLabel.appendChild(nsCheckbox);
    nsCheckboxLabel.appendChild(document.createTextNode(' Invert selection'));
    nsForm.appendChild(document.createTextNode(' ('));
    nsForm.appendChild(nsCheckboxLabel);
    nsForm.appendChild(document.createTextNode(') '));

    wlNotePara.appendChild(document.createElement('br'));
    wlNotePara.appendChild(nsForm);

    prepareFilter();
    activateFilter();
});

// </nowiki></pre>