Wikipedia:WikiProject User scripts/Scripts/Unwatch

This is an old revision of this page, as edited by Ilmari Karonen (talk | contribs) at 03:42, 5 January 2006 (fix bug in enhanced mode). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

// This script adds an "unwatch" link to each entry in your watchlist. This version works regardless of whether you have the "Enhanced recent changes" option selected in your user preferences.


addOnloadHook(function () {
    if (window.___location.href.indexOf("Special:Watchlist") == -1) return;

    // look for h4 followed by div/ul
    var headings = document.getElementById('content').getElementsByTagName('h4');

    for (var i = 0; i < headings.length; i++) {
        var div = headings[i].nextSibling;
        while (div && div.nodeType == 3) div = div.nextSibling;  // skip text nodes
        if (!div || !div.tagName) continue;

        if (div.tagName.toLowerCase() == 'div') {  // enhanced recent changes
            // this is all tag soup, we just have to wade through it :-(
            for (var node = div.firstChild; node; node = node.nextSibling) {
                // locate the history link
                if (!node.tagName || node.tagName.toLowerCase() != 'a' || node.href.substring(node.href.length-15) != '&action=history')
                    continue;
                var pagename = node.title;
                var unwatch = document.createElement('a');
                unwatch.href = "/w/index.php?title=Special:Watchlist&action=submit&remove=1&id[]="+encodeURIComponent(pagename);
                unwatch.title = "Unwatch "+pagename;
                unwatch.appendChild(document.createTextNode("unwatch"));
                div.insertBefore(unwatch, node.nextSibling);
                div.insertBefore(document.createTextNode("; "), unwatch);
            }
        }
        else if (div.tagName.toLowerCase() == 'ul') { // unenhanced recent changes
            var items = div.getElementsByTagName('li');
            for (var j = 0; j < items.length; j++) {
                var links = items[j].getElementsByTagName('a');
                var pagename = links[0].title;
                var unwatch = document.createElement('a');
                unwatch.href = "/w/index.php?title=Special:Watchlist&action=submit&remove=1&id[]="+encodeURIComponent(pagename);
                unwatch.title = "Unwatch "+pagename;
                unwatch.appendChild(document.createTextNode("unwatch"));
                items[j].insertBefore(unwatch, links[1].nextSibling);
                items[j].insertBefore(document.createTextNode(") ("), unwatch);
            }
        }
    }
});

//