Jump to content

Wikipedia:WikiProject User scripts/Scripts/Watchfilter

From Wikipedia, the free encyclopedia

/* This script lets you filter your watchlist by namespace, similar to recent changes.

This script is now mostly obsolete, as similar functionality has been added to MediaWiki. It is no longer actively maintained.

Known bugs:

  • Currently the filter is reset whenever you reload the page or follow the "show last X hours/days" or "show/hide my edits" links. The latter could be fixed fairly easily, but the obvious fix for the former would cause a noticeable slowdown.
  • The script no longer works if "Enhanced Recent Changes" has been enabled, due to changes in the enhanced watchlist page layout since the script was written.

*/ //

addOnloadHook(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();
});

//