Wikipedia undelete

By Johan Sundström Last update Jan 4, 2008 — Installed 178 times.
// ==UserScript==
// @name           Wikipedia undelete
// @url            http://userscripts.org/scripts/source/19097.user.js
// @namespace      http://code.google.com/p/ecmanaut/
// @description	   Salvages deleted Wikipedia articles from archive.org, as you encounter them, and shows when and by whom the article was killed.
// @include        http://*.wikipedia.org/*
// ==/UserScript==

var deletedWhen = /<li>([0-9:]{5}, \d+ \S+ \d{4,} .*? deleted .*?)<\/li>/;
var archivedOld = /<a href="(.*?)">(\S+ \d{1,2}, \d{4,})<\/a> \*/g;
var not = '//b[.="Wikipedia does not have an article with this exact name."]';
var del = '//a[.="deletion log"]';

if ((not = $X(not)) && (del = $X(del))) {
  append(not, <br/>);
  get( del.href, whowhen );
}

function get(url, cb) {
  GM_xmlhttpRequest({ method:'GET', url:url,
                      onload:function(xhr) { cb( xhr.responseText ); }});
}

function append(target, e4x) {
  return target.parentNode.insertBefore(e2d(e4x), target.nextSibling);
}

function e2d(e4x, doc) {
  e2d.parser = e2d.parser || new DOMParser;
  var xml = <testing xmlns="http://www.w3.org/1999/xhtml"/>;
  xml.tree = e4x;
  var dom = e2d.parser.parseFromString(xml.toXMLString(), "text/xml");
  var tree = dom.documentElement.firstChild;
  while (tree && tree.nodeType != 1)
    tree = tree.nextSibling;
  return tree ? (doc || document).importNode( tree, true ) : null;
}

function $X( xpath, root ) {
  var got = $x( xpath, root );
  return got instanceof Array ? got[0] : got;
}

// list nodes matching this expression, optionally relative to the node `root'
function $x( xpath, root ) {
  var doc = root ? root.evaluate ? root : root.ownerDocument : document, next;
  var got = doc.evaluate( xpath, root||doc, null, null, null ), result = [];
  switch( got.resultType ) {
    case got.STRING_TYPE:
      return got.stringValue;
    case got.NUMBER_TYPE:
      return got.numberValue;
    case got.BOOLEAN_TYPE:
      return got.booleanValue;
    default:
      while( next = got.iterateNext() )
	result.push( next );
      return result;
  }
}

function whowhen(html) {
  var found = html.match(deletedWhen);
  if (found) {
    append(not, new XMLList("<b>"+found[1]+"</b>"));
    get("http://web.archive.org/web/*/" + location.href, lastgood);
  }
}

function lastgood(html) {
  var old = [], urls = {}, found;
  while ((found = archivedOld.exec(html))) {
    var date = found[2], url = found[1];
    old.push(date);
    urls[date] = url;
  }
  if (url) {
    var ul = <ul/>;
    while ((date = old.pop())) {
      url = urls[date];
      ul.* += <li><a href={url}>{date}</a></li>;
    }
    append(not, <p>{ul}</p>);
    append(not, <b>Old version{old.length == 1 ? "" : "s"} saved for posterity
           thanks to <a href="http://www.archive.org/">archive.org</a>:</b>);
    append(not, <br/>);
  }
}