User:Gutza/quickUnwatch dev.js

// 2008, ro:Utilizator:Gutza, GPL

var quickUnwatch={ linkText: "-w", fullText: "(%s) ", format: "u^adh" };

quickUnwatch.onLoad = function { quickUnwatch.form={ u: quickUnwatch.format.indexOf("u"), a: quickUnwatch.format.indexOf("a"), d: quickUnwatch.format.indexOf("d"), h: quickUnwatch.format.indexOf("h"), c: quickUnwatch.format.indexOf("^") }; quickUnwatch.form.side=quickUnwatch.form.c>quickUnwatch.form.u?"before":"after"; quickUnwatch.form.ref=quickUnwatch.format.substr(quickUnwatch.form.c+(quickUnwatch.form.side=="before"?1:-1),1); if (   quickUnwatch.form.u==-1 ||    quickUnwatch.form.a==-1 ||    quickUnwatch.form.d==-1 ||    quickUnwatch.form.h==-1 ||    quickUnwatch.form.c==-1 ||    quickUnwatch.form.ref=="" ||    Math.abs(quickUnwatch.form.u-quickUnwatch.form.c)!=1  ) { alert("Bad format!\nquickUnwatch.format is poorly formatted!"); return false; } if (quickUnwatch.form.u>quickUnwatch.form.c) { quickUnwatch.form.u--; } if (quickUnwatch.form.a>quickUnwatch.form.c) { quickUnwatch.form.a--; } if (quickUnwatch.form.h>quickUnwatch.form.c) { quickUnwatch.form.h--; } if (quickUnwatch.form.d>quickUnwatch.form.c) { quickUnwatch.form.d--; } quickUnwatch.unwatching=false; if (!quickUnwatch.validPage) { return false; } var links=document.getElementsByTagName('a'); var prevlink='n/a'; var curlink='n/a'; var linkdelta=2; for(var i=0;i<links.length;i++) { if (links[i].href.indexOf('action=history')==-1) { if (links[i].href.indexOf('&diff=')!=-1) { curlink='diff'; } else { curlink='n/a'; }   } else { curlink='hist'; }   if (curlink=='hist') { if (prevlink=='diff') { linkdelta=2; } else { linkdelta=1; }     var article_link=links[i-linkdelta]; var article_name=article_link.innerHTML; var newnode=document.createElement("span"); newnode.innerHTML=quickUnwatch.fullText.replace("%s",""+quickUnwatch.linkText+""); article_link.parentNode.insertBefore(newnode,article_link); i++; }   prevlink=curlink; } }

quickUnwatch.unwatch = function(article) { if (quickUnwatch.unwatching) { alert("Already unwatching!"); return false; } quickUnwatch.unwatching=true; if (wgCanonicalSpecialPageName != "Watchlist") { return false; } var atoms=quickUnwatch.getAtoms(article); if (!atoms) { return false; } atoms.unwatch.innerHTML="…"; sajax_do_call(   "wfAjaxWatch",    [atoms.article.innerHTML, "u"],     quickUnwatch.processResult  ); // if the request isn't done in 10 seconds, allow user to try again wgAjaxWatch.timeoutID = window.setTimeout(   function { quickUnwatch.unwatching = false; quickUnwatch.restoreLink(atoms.article.innerHTML); },    10000  ); }

quickUnwatch.processResult = function(request) { var response = request.responseText; if( response.match(/^/) ) { window.location="/w/index.php?title=Special:Watchlist&action=purge"; } else { // Either we got a  error code or it just plain broke. alert("Unwatch failed, please try again!"); quickUnwatch.unwatching = false; return; } if(quickUnwatch.timeoutID) { window.clearTimeout(quickUnwatch.timeoutID); } }

quickUnwatch.restoreLink = function(article) { if (wgCanonicalSpecialPageName != "Watchlist") { return false; } var atoms=quickUnwatch.getAtoms(article); if (!atoms) { return false; } atoms.unwatch.innerHTML=quickUnwatch.linkText; }

quickUnwatch.getAtoms = function(article) { var links=document.getElementsByTagName('a'); var prevlink='n/a'; var linkdelta=2; for(var i=0;i<links.length;i++) { if (links[i].href.indexOf('action=history')==-1) { if (links[i].href.indexOf('&diff=')!=-1) { prevlink='diff'; } else { prevlink='n/a'; }     continue; }   if (prevlink=='diff') { linkdelta=2; } else { linkdelta=1; }   var article_link=links[i-linkdelta]; prevlink='hist'; var article_name=article_link.innerHTML; if (article_name!=article) { continue; }   return { article: links[i-linkdelta], unwatch: links[i-linkdelta-1] }; }  return false; }

quickUnwatch.validPage = function { if (wgCanonicalSpecialPageName != "Watchlist") { return false; } var tmp=new String(window.location); if (tmp.indexOf("Special:Watchlist/")!=-1) { return false; } return true; }

hookEvent("load", quickUnwatch.onLoad);