Wikipedia:WikiProject User scripts/Scripts/Watchfilter

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

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

//

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.