Jump to content

User:PSaxena (WMF)/ipinfo.js

From Wikipedia, the free encyclopedia
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.
function showInfo (data) {
	console.log(data);
	var proxyClass = data.isProxy ? 'green-dot' : 'red-dot';
	var proxyText = data.isProxy ? 'Not a proxy' : 'Known proxy';
	
	$('#mw-content-text').prepend(
		$('<div>').addClass('ipinfo').append(
			$('<img>').addClass('ipinfo-map').attr('src',data.map),
			$('<p>').text(data.addr + ', ' + data.loc)
				.append('<br>')
				.append(data.org),
			$('<code>').text(data.host),
			$('<p>').append(
				$('<span>').addClass('dot').addClass(proxyClass),
				proxyText
			)
		)
	);
};

// From https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#ECMAScript_.28JavaScript.2FActionScript.2C_etc..29
function lon2tile(lon,zoom1) { 
	tt = Number(lon);
	return (Math.floor((tt+180)/360*Math.pow(2,zoom1)));
}

function lat2tile(lat,zoom2)  { 
    return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom2))); 
}

function getInfo() {
	var hrefArray = window.location.href.split('/');
	var ip = hrefArray[hrefArray.length - 1];
	fetch('https://ipinfo.io/'+ip+'/json').then(function(data){
      	return data.json();
      }).then(function(json){
      	var data = {};
      	// From IP Info
      	data.addr = json.city + ' ' + json.postal;
      	data.loc = json.region + ' ' + json.country;
      	data.org = json.org;
      	data.host = json.hostname;
      	
      	// Map
      	var lat = json.loc.split(',')[0];
      	var lon = json.loc.split(',')[1];
      	var x = lon2tile(lon,10);
      	var y = lat2tile(lat,10);
		data.map = 'https://maps.wikimedia.org/osm-intl/10/'+x+'/'+y+'@2x.png'
      	
      	// Dummy data
      	data.isProxy = (parseInt(ip) % 2 === 0);
      	
      	showInfo(data);
      });
}

$(document).ready(function() {
	if (window.location.href.indexOf('Special:Contributions') > -1 ) {
		getInfo();
	}
});