User:PSaxena (WMF)/ipinfo.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:PSaxena (WMF)/ipinfo. This user script seems to have an accompanying .css page at User:PSaxena (WMF)/ipinfo.css. |
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();
}
});