// ==UserScript==
// @name Show IP
// @author Lex1 (based on version for FF from Michael Klaus)
// @version 1.1.7
// @description  Show yours and site's IP-address.
// @include http://*
// @ujs:documentation http://ruzanow.ru/index/0-5
// @ujs:download http://ruzanow.ru/userjs/show-ip.js
// ==/UserScript==

(function(){

var frId = 'ujs_show-ip';

var ujsWin = function(txt){
	var wId = 'ujs_show_ip_windows';
	var div = document.getElementById(wId);
	if(div){
		div.appendChild(document.createTextNode(';   '+txt));
	}
	else{
		div = document.createElement('div');
		div.id = wId;
		div.setAttribute('style', 'display:block;position:fixed;left:0;bottom:0;width:auto;height:auto;background:-o-skin("Window Skin");font:16px Times New Roman;border:1px solid gray;padding:2px;z-index:9999;opacity:0.9;white-space:pre;');
		var img = document.createElement('img');
		img.title = 'Close';
		img.setAttribute('style', 'display:block;position:relative;float:right;height:18px;width:18px;margin:1px 0 1px 2px;background:-o-skin("Caption Close Button Skin");cursor:pointer;');
		img.onclick = 'this.parentNode.parentNode.removeChild(this.parentNode)';
		div.appendChild(img);
		div.appendChild(document.createTextNode(txt));
		document.documentElement.appendChild(div);
	}
};

window.addEventListener('message', function(e){
	if(e.data == 'getip' && location.hostname == 'www.tsql.de'){
		e.source.postMessage('get_ip' + document.getElementsByTagName('showmyip')[0].getAttribute('ip'), '*');
	}
	else if(e.data && e.data.indexOf('get_ip') == 0 && (e.origin || 'http://'+e.domain) == 'http://www.tsql.de'){
		var rez = e.data.substr(6);
		var fr = document.getElementById(frId);
		if(fr)fr.parentNode.removeChild(fr);
		if(/^(?:\d{1,3}\.){3}\d{1,3}$/.test(rez))ujsWin('My IP: ' + rez);
	}
}, false);

window.addEventListener('load', function(){
	if(window.parent != window)return;
	// My IP
	var f = document.getElementById(frId);
	if(!f)f = document.createElement('iframe');
	f.width = 0;
	f.height = 0;
	f.frameBorder = 'no';
	f.scrolling = 'no';
	f.id = frId;
	f.src = 'http://www.tsql.de/ipxml.php';
	f.onload = function(){(window.postMessage ? f.contentWindow : f.contentDocument).postMessage('getip', '*')};
	document.documentElement.appendChild(f);
	// IP for site
	if(typeof(java) == 'object' && java.net)ujsWin('IP: ' + java.net.InetAddress.getByName(location.hostname).getHostAddress());

	// Both IP with Java
	//if(!navigator.javaEnabled()){alert('Java not enabled or not installed!');return};
	//var host=location.hostname;
	//var port=location.port || 80;
	//ujsWin('IP: '+java.net.InetAddress.getByName(host).getHostAddress());
	//ujsWin('My IP: '+(new java.net.Socket(host,port)).getLocalAddress().getHostAddress());
},false);

})();
