Jump to content

User:Lupin/recentdiffs.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lupin (talk | contribs) at 03:18, 1 November 2005. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// <pre><nowiki>

// **************************************************
// Downloader
// **************************************************

function Downloader(url) {
  // Source: http://jibbering.com/2002/4/httprequest.html
  this.http = false;

  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, 
  // we can cope with old IE versions.
  // and security blocked creation of the objects.
  try {
  this.http = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
  try {
  this.http = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
  // this.http = false;
  }
  }
  @end @*/

  if (! this.http && typeof XMLHttpRequest!='undefined') this.http = new XMLHttpRequest();

  this.url = url; this.id=null;
  this.lastModified = null;
  this.callbackFunction = null;
};

Downloader.prototype.send = function (x) {if (!this.http) return null; return this.http.send(x);};
Downloader.prototype.abort = function () {if (!this.http) return null; return this.http.abort();};
Downloader.prototype.runCallback = function () {this.callbackFunction(this);};
Downloader.prototype.getData = function () {if(!this.http) return null; return this.http.responseText;};
Downloader.prototype.setTarget = function () {if(!this.http) return null; this.http.open("GET", this.url, true);};
Downloader.prototype.start=function () {if(!this.http) return null; return this.http.send(null);};
Downloader.prototype.getReadyState=function () {if(!this.http) return null; return this.http.readyState;};

Downloader.prototype.getLastModifiedDate=function () {
  if(!this.http) return null; 
  var lastmod=null;
  try {
    lastmod=this.http.getResponseHeader('Last-Modified'); 
  } catch (err) {}
  if (lastmod) return new Date(lastmod);
  return null;
}

Downloader.prototype.setCallback = function (f) { 
  if(!this.http) return;
  this.http.onreadystatechange = f;
  this.callbackFunction = f;
};

Downloader.newDownload=function(url, id, callback) {
  var d=new Downloader(url);
  if (!d.http) return 'ohdear';
  d.id=id;
  d.setTarget();
  var f = function () {
    if (d.getReadyState() == 4) { 
      d.data=d.getData(); 
      d.lastModified=d.getLastModifiedDate();
      callback(d);
    }
  };
  d.setCallback(f);
  return d;//d.start();
};

Downloader.startDownload=function(url, id, callback, userData) {
  var d=Downloader.newDownload(url, id, callback); 
  d.userData=userData;
  if (typeof d == typeof '' ) return d;
  return d.start();
};

// **************************************************
// DOM abbreviations
// **************************************************

$a = function (obj, parent) { 
  var p=(parent)?parent:document.body; 
  if (typeof obj== 'object' && 
      obj.length != null && obj.length > 0 && 
      typeof obj[0] == 'object' && obj[0].nodeType!=null) {
      // assume it's a list of nodes
    for (var i=0; i<obj.length; ++i) {
      p.appendChild(obj[i]);
    }
  } else p.appendChild(obj); 
}
$e=function (id) {return document.getElementById(id);};
$c=function(elt) {return document.createElement(elt);},
$t=function(txt) {return document.createTextNode(txt);};
$ee=function(nm, parent) {var p=(parent)?parent:document; return p.getElementsByTagName(nm);};

// **************************************************
// Recentchecker
// **************************************************

function Recentchecker (auto) {
  this.badWords=[
                 'wanker',
                 'never',
                 /\bf.ck\b/i,
                 /\bsh[^ou]t\b/i
  ];
  this.init();
  if (auto) { this.getPages(); this.checkPages(); }
}

Recentchecker.prototype.init = function () {
  this.div=$c('div');
  this.div.setAttribute('id', 'recentCheckerDiv');
  var title=document.getElementsByTagName('h1')[0];
  title.parentNode.insertBefore(this.div, title.nextSibling);
}

Recentchecker.prototype.appendToDiv = function (stuff) {
  var d=this.div;
  $a(stuff, d);
}

Recentchecker.prototype.print=function (str) {
  this.appendToDiv([$t(str), $c('br')]);
}

Recentchecker.prototype.getDiffs=function () {
  var ret=[];
  for (var i=0; i<document.links.length; ++i) {
    if (document.links[i].innerHTML=='diff') ret.push(document.links[i]);
  }
  this.diffPages=ret;
}

Recentchecker.prototype.getPages=function () {
  // heuristic: for each <H4> (date heading), we look inside the next
  // div.  this should contain the list of links. the article links are
  // the first link and then the links following a <br>
  var ret=[];
  
  var dateHeadings=$ee('h4');
  for (var i=0; i<dateHeadings.length; ++i) {
    var d=h=dateHeadings[i];
    while ( ! isNodeName(d,'DIV') ) { d=d.nextSibling; }
    if (! d) continue;
    // look inside the div d
    var firstChild=d.childNodes[0];
    var justSeenBr=true;
    for (var a=firstChild; a=a.nextSibling; ) {
      var aa=a;
      if (isNodeName(a, 'BR')) { justSeenBr=true; continue; }
      if (isNodeName(a, 'strong')) { aa=a.childNodes[0]; }
      if ( (isNodeName(aa, 'A') && justSeenBr)) {
        justSeenBr=false;
        ret.push(aa);
      }
    }
  }
  this.pages=ret;
}


Recentchecker.prototype.checkDiffs=function () {
  if (this.diffPages==null) this.getDiffs();
  for (var i=0; i<this.diffPages.length; ++i) {
    var savedThis=this;
    var f=function(d) { savedThis.diffHandler.apply(savedThis, [d]); };
    Downloader.startDownload(this.diffPages[i].href, 
                             0, f, {sourceNode: this.diffPages[i]});
  }
}

Recentchecker.prototype.diffHandler=function (d) {
  var data=d.data;
  var diffTable=/<table[^>]*?class=.diff.[^>]*?>(.|\n)*?<\/table>/.exec(data)[0];
  var diffNode=d.userData.sourceNode;
  var newDiv=$c('div'); newDiv.innerHTML=diffTable;
  var nextBrSib=nextSiblingOfType(diffNode, 'br');
  if (nextBrSib && nextBrSib.nextSibling) { 
    nextBrSib = nextBrSib.nextSibling; 
    diffNode.parentNode.insertBefore(newDiv, nextBrSib);
  } else diffNode.parentNode.appendChild(newDiv);
}

function isNodeName(node, str) { 
  return node.nodeName.toUpperCase() == str.toUpperCase(); 
}

function nextSiblingOfType(node, type) {
  for (var n=node; n; n=n.nextSibling) {
    if (isNodeName(n, type)) return n;
  }
  return null;
}

function checkRecentDiffs() {
  if (location.href.indexOf('Special:Recentchanges') > -1 ) {
    new Recentchecker().checkDiffs();
  }
}

function addlilink(tabs, url, name, id, title, key){
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    if(id)
    {
        if(key && title)
        {
            ta[id] = [key, title];
        }
        else if(key)
        {
            ta[id] = [key, ''];
        }
        else if(title)
        {
            ta[id] = ['', title];
        }
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();
    return li;
}

function addToolboxLink(url, name, id){
    var tb = document.getElementById('p-tb').getElementsByTagName('ul')[0];
    addlilink(tb, url, name, id);
}

function addPowerdiffLink() {
  addToolboxLink('#', 'Show all diffs', 'toolbox_alldiffs');
  $e('toolbox_alldiffs').onclick=checkRecentDiffs;
}

if (window.addEventListener) {
  window.addEventListener("DOMContentLoaded",addPowerdiffLink,false) || window.addEventListener("load", addPowerdiffLink, false);
}
else if (window.attachEvent) {
  window.attachEvent("onload",addPowerdiffLink);
}
else {
  window._old_recent_onload = window.onload;
  window.onload = function() {
    window._old_recent_onload();
    addPowerdiffLink();
  }
}

// </nowiki></pre>

/// Local Variables: ///
/// mode:c ///
/// fill-prefix:"// " ///
/// End: ///