MediaWiki:Gadget-CommentWatch.js

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

try { // containerize gadget, to protect other gadgets if this one goes wrong

//This is a very quick script that is not very good. Will make nicer later. YMMV with this.
/*extern sajax_init_object */
if (mw.config.get('wgNamespaceNumber') === 0 || mw.config.get('wgNamespaceNumber') === 1 || mw.config.get('wgNamespaceNumber') === 102) {
 var cWatch = {}; //init obj

 cWatch.init = function () {
 var Page = "Comments:" + mw.config.get('wgPageName'); // default
 if (mw.config.get('wgNamespaceNumber') === 1) {
  Page = "Comments:" + mw.config.get('wgPageName').split(":", 2)[1];
 }
 else if (mw.config.get('wgNamespaceNumber') === 102) {
  Page = mw.config.get('wgPageName').split(":", 2)[1];
 }

 cWatch.watchComments = function () {
   var x;
   x = sajax_init_object();
 
   // do nothing if we can't check
   if (!x) {
     return;
   }
 
   x.open("GET", "//en.wikinews.org/w/index.php?action=watch&title=" + Page, true);
   x.setRequestHeader("Pragma", "cache=yes");
   x.setRequestHeader("Cache-Control", "no-transform");
   x.onreadystatechange = function () {
    if (x.readyState === 4) {
    /* Start success*/
    if (x.status === 200) {
     document.getElementById("siteNotice").innerHTML += ('Comment Page now watched' );
    }
    else {
     document.getElementById("siteNotice").innerHTML += ('Auto comment page watch <b>failed</b>. <a href="//en.wikinews.org/w/index.php?action=watch&title=' + Page + '">Watch manually?</a>.' );
    }
    if (document.getElementById("ca-watch").addEventListener) {
     document.getElementById("ca-watch").firstChild.removeEventListener("click", cWatch.watchComments, true);
    }
    else if (document.getElementById("ca-watch").firstChild.attachEvent) {
     document.getElementById("ca-watch").firstChild.detachEvent("onclick", cWatch.watchComments);
    }
    x = undefined;
   }
  }
  x.send(null);
 }


 if (document.getElementById("ca-watch")) { //In case we're already watching
  var watchTab = document.getElementById("ca-watch").firstChild;
  if (watchTab.addEventListener) {
   watchTab.addEventListener("click", cWatch.watchComments, true);
  }
  else if (watchTab.attachEvent) {
   watchTab.attachEvent("onclick", cWatch.watchComments);
  }
  else {
   document.getElementById("siteNotice").innerHTML += ('Auto comment page watch <b>failed</b>. <a href="//en.wikinews.org/w/index.php?action=watch&title=' + Page + '">Watch manually?</a>.' );
  }
 }



 } //End cWatch.init


 if (document.getElementById("globalWrapper")) { //We're a load event
  cWatch.init();
 }
  else {
  $(cWatch.init);
 }
}

} catch (e) { // containerize gadget, to protect other gadgets if this one goes wrong
  // ignore
}