Wikipedia:WikiProject User scripts/Scripts/Unwatch

This is an old revision of this page, as edited by Jnothman (talk | contribs) at 02:29, 5 January 2006 (provide two variations). 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.

There are two variations, depending on whether the "Enhanced recent changes" option is selected in your user preferences.

No enhanced recent changes

// Unwatch link: No enhanced recent changes
// Add Unwatch to watch list
// from [[User:Matthewmayer/monobook.js]]
addOnloadHook(function () {
   if (window.___location.href.indexOf("Special:Watchlist")!=-1) {
      //correct page, so retrieve watchlist items
      var items=document.getElementById('bodyContent').getElementsByTagName('li');
      for (var i=0;i<items.length;i++) {
            //what is the title of this page?
            pagename=items[i].getElementsByTagName('a')[0].title;
            //create 'unwatch' element
            unwatchelt=document.createElement('a');
            unwatchelt.setAttribute('href','/w/index.php?title='+pagename+'&action=unwatch');
            unwatchelt.setAttribute('title','Unwatch '+pagename);
            unwatchelt.appendChild(document.createTextNode('(unwatch)'));
            //add the 'unwatch' element
            items[i].appendChild(unwatchelt);
      }
    }
 }
);
//

Enhanced recent changes

// Unwatch link: Enhanced recent changes
addOnloadHook(function () {
    if (window.___location.href.indexOf("Special:Watchlist") == -1) return;

    // this is all tag soup, we just have to wade through it :-(
    // look for h4 followed by div
    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.toLowerCase() != 'div') continue;

        var pagename = "";
        for (var node = div.firstChild; node; node = node.nextSibling) {

            // grab the title of the first link on each line
            if (!pagename && node.tagName && node.tagName.toLowerCase() == 'a')
                pagename = node.title;
            if (pagename && node.tagName && node.tagName.toLowerCase() == 'br')
                pagename = "";

            // add the (unwatch) link after diff and hist (look for distinctive text node)
            if (pagename && node.nodeType == 3 && node.nodeValue == ") . . ") {
                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(document.createTextNode("; "), node);
                div.insertBefore(unwatch, node);
            }
        }
        // XXX: might want to double check that pagename is null here, but I'll ignore it for now
    }
});

//