// ==UserScript==
// @name           Webalizer IP lookup
// @namespace      http://djce.org.uk/greasemonkey
// @description    Within a webalizer page, provide links to look up IP addresses
// @include        *
// ==/UserScript==

try {
	var append_ip_link = function(e, href, label) {
		var a = document.createElement("a");
		a.setAttribute("style", "margin-left: 0.5em");
		a.setAttribute("href", href);
		var t = document.createTextNode(label);
		a.appendChild(t);
		e.appendChild(a);
	};

	var found_ip = function(e, ip) {
		append_ip_link(e, "http://www.google.com/search?q="+ip, "Google");
		append_ip_link(e, "http://sitedossier.com/ip/"+ip, "sitedossier");
		append_ip_link(e, "http://whois.gwebtools.com/"+ip, "gwebtools");
		append_ip_link(e, "http://www.robtex.com/ip/"+ip+".html", "robtex");
	};

	var find_ips = function() {
		var x = document.evaluate(
			'//tr/td[last()]',
		       document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
		);
		var n = 0;
		for (var i=0; i<x.snapshotLength; ++i)
		{
			var e = x.snapshotItem(i);
			var t = e.innerHTML;
			var m = t.match(/\b(\d+\.\d+\.\d+\.\d+)\b/);
			if (!m) continue;
			found_ip(e, m[1]);
		}
	};

	var is_webalizer = function() {
		var head = document.getElementsByTagName("head");
		if (head.length != 1) return;
		head = head[0];
		var m = head.innerHTML.match("Generated by The Webalizer");
		if (!m) return;
		return true;
	};

	if (is_webalizer()) find_ips();
} catch(e) {
	alert("Error in webalizer_ip_lookup: "+e);
}

