Jump to content

User:Gutza/quickUnwatch.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// 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);