// This is a Greasemonkey User Script
// Tim Hodson
// http://www.timhodson.com
// tim@timhdson.com
// Feel free to use this any way you like, 
// with the restriction that you notify me of any changes you make to make it better!


// ==UserScript==
// @name		Reverse DNS - Awstats
// @namespace 	http://www.timhodson.com
// @description	Show a link to a reverse lookup service after an IP address in awstats.  Will also work with any IP address found inside a table cell if you include the address.
// @include	*awstats.pl*
// ==/UserScript==

window.addEventListener(
	'load',
	function(){
		// <td class="aws">209.61.157.78</td> this be our candidate!
		var candidates
		candidates = document.getElementsByTagName('td');
		if (candidates){
			for (var i=0; i<candidates.length; i++){
				var candidate = candidates[i];
				if (candidate.className.indexOf('aws')>=0){
					if (candidate.hasChildNodes()){
							// now look for IP num
							var str = candidate.firstChild.textContent
							var regexp = /(\d{1,3}\.){3}/g;
							var result = str.match(regexp);
							if (result){
								// add a lookup link
								var linkBase = 'http://www.dnsstuff.com/tools/ptr.ch?ip=';
								var IP = str;
								
								var anchor = document.createElement('a');
								var stxt = document.createTextNode(' - '); 
								
								var atxt = document.createTextNode('Reverse lookup');
								anchor.setAttribute('class','RDNS');
								anchor.target = 'new';
								anchor.href = linkBase + IP;
								anchor.appendChild(atxt);
								
								candidate.appendChild(stxt);
								candidate.appendChild(anchor);
								
								
							} 
					}
				}
			}
		GM_log('finished');
		}
	},
	true);

