User:Gutza/quickUnwatch.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
// 2008, [[:ro:Utilizator:Gutza]], GPL
 
 
quickUnwatch=new Object;
 
quickUnwatch.linkText="-w";
 
quickUnwatch.onLoad = function()
{
  quickUnwatch.unwatching=false;
  if (!quickUnwatch.validPage()) {
    return false;
  }
  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;
    }
    prevlink='hist';
    var article_link=links[i-linkdelta];
    var article_name=article_link.innerHTML;
    var newnode=document.createElement("span");
    newnode.innerHTML="(<a href='#' onClick='quickUnwatch.unwatch(\""+escape(article_name)+"\"); return false'>"+quickUnwatch.linkText+"</a>) ";
    article_link.parentNode.insertBefore(newnode,article_link);
    i++;
  }
}
 
quickUnwatch.unwatch = function(article)
{
  if (quickUnwatch.unwatching) {
    alert("Already unwatching!");
    return false;
  }
  quickUnwatch.unwatching=true;
  if (wgCanonicalSpecialPageName != "Watchlist") {
    return false;
  }
  article=unescape(article);
  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(/^<u#>/) ) {
    window.location="/w/index.php?title=Special:Watchlist&action=purge";
  } else {
    // Either we got a <err#> 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);