User:Vyom25/unwatch

/* This script adds an "unwatch" link to each entry in your watchlist, using the new * ajax watchlist thing. */

var wlUnwatch = { supported:true, onLoad:function{ var f=document.getElementById('mw-watchlist-form-namespaceselector'); if(!f || !f.elements.namespace) return; var links=document.getElementById('content').getElementsByTagName('a'); var j;       for(var i=links.length-1; i>=0; i--){ if(/[?&]action=history([&#]|$)/.test(links[i].href)){ j=i; } else if(/\/Special:Log\//.test(links[i].href)){ j=i+4; } else { continue; }           var m=links[j].href.match(/title=([^&]*)/); if(!m) continue; var a=document.createElement('A'); a.href=mw.util.wikiScript+'?action=unwatch&title='+m[1]; a.title='Unwatch '+links[j].title; a.unwatchInProgress=false; a.onclick=wlUnwatch.onClick; a.appendChild(document.createTextNode('unw')); links[i].parentNode.insertBefore(a, links[i].nextSibling); // insert a delimiter between the two links var d=document.createTextNode('\xa0| '); links[i].parentNode.insertBefore(d, a); }   },    onClick:function{ if (!wlUnwatch.supported || !mw.config.get('wgEnableWriteAPI')) { wlUnwatch.supported = false; return true; }       var link = this; if(link.unwatchInProgress) return false; link.unwatchInProgress=true; link.style.color='red'; var timeout=setTimeout(function{           timeout=null;            link.unwatchInProgress=false;            link.style.color='';        }, 10000); $.ajax({           url:mw.util.wikiScript('api'),            dataType:'json',            type:'POST',            data:{                action:'watch',                format:'json',                unwatch:1,                token:mw.user.tokens.get('watchToken'),                title:decodeURIComponent(link.href.match(/&title=(.+)/)[1].replace(/_/g,' '))            },            success:function(r, sts, xhr){                if(timeout) clearTimeout(timeout);                timeout=null;                link.unwatchInProgress=false;                link.style.color='';                if(r.watch.unwatched !== undefined){                    var li;                    for(li=link; li && li.nodeName!='LI'; li=li.parentNode);                    if(li) li.parentNode.removeChild(li);                }            },            error:function{                if(timeout) clearTimeout(timeout); timeout=null; link.unwatchInProgress=false; link.style.color=''; }       });        return false;    } }; if(mw.config.get('wgCanonicalSpecialPageName')=='Watchlist' && mw.config.get('wgAction')=='view'){    mw.loader.using(['mediawiki.util','mediawiki.user'], function{ $(document).ready(wlUnwatch.onLoad); }); }