Jump to content

User:Spang/autowatch.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.
/* 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', mw.config.get('wgServer') + mw.config.get('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);