Wikipedia:WikiProject User scripts/Scripts/Unwatch

This is an old revision of this page, as edited by Jnothman (talk | contribs) at 03:52, 5 January 2006 (refactor a little). 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;
    
    function unwatchLink(pagename) {
        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"));
        return unwatch;
    }

    // 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 unwatch = unwatchLink(node.title);
                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 unwatch = unwatchLink(links[0].title);
                items[j].insertBefore(unwatch, links[1].nextSibling);
                items[j].insertBefore(document.createTextNode(") ("), unwatch);
            }
        }
    }
});

//