User:Spang/autowatch.js

From Wikipedia, the free encyclopedia

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.

/* Allows you to watch/unwatch a page without leaving it. */
 
function getXmlHttpObject() {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        xmlHttp = false;
      }
    }
  }
  return xmlHttp;
}
 
function changeTab() {
if (xmlHttp.readyState != 2) return;
   if (watchAction == 'watch') {
      watchLink.innerHTML = 'unwatch';
      watchAction = 'unwatch';
   } else if (watchAction == 'unwatch') {
      watchLink.innerHTML = 'watch';
      watchAction = 'watch';
   }
}
 
function setWatch(action) {
   xmlHttp = getXmlHttpObject();
   if (xmlHttp == false) return;
   xmlHttp.onreadystatechange = changeTab;
   xmlHttp.open('GET', wgServer + wgScript + '?title=' + wgPageName + '&action=' + action, true);
   xmlHttp.send(null);
}
 
function setWatchTab() {
   if (document.getElementById('ca-watch') || document.getElementById('ca-unwatch')) {
      if (document.getElementById('ca-watch')) {
         watchLink = document.getElementById('ca-watch').getElementsByTagName('a')[0];
         watchAction = 'watch';
      } else if (document.getElementById('ca-unwatch')) {
         watchLink = document.getElementById('ca-unwatch').getElementsByTagName('a')[0];
         watchAction = 'unwatch';
      }
      watchUrl = watchLink.href
      watchLink.href = 'javascript:setWatch(watchAction);';
   }
}
addOnloadHook(setWatchTab);