/
whoshere.js
86 lines (54 loc) · 1.86 KB
/
whoshere.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var whoshere = function( customCallBack ) {
var win = window,
nav = navigator;
this.getInfo = function() {
this.getIPAddress();
};
this.getIPAddress = function() {
var req = new XMLHttpRequest(),
self = this,
formatJSONP = function( str ) { return JSON.parse( str.substring( str.indexOf('(') + 1, str.lastIndexOf( ')') ) ) };
req.open( 'GET', 'http://freegeoip.net/json/?callback=?', true );
req.send();
req.onload = function() {
var jsonp = req.responseText;
self.getCurrentPosition( navigator, formatJSONP(jsonp) );
};
};
this.returnInfo = function( posValue, ip ) {
var date = new Date(),
obj = {
platform: nav.platform,
lang: nav.language,
onLine: nav.onLine,
currentLocation: posValue,
doNotTrack: nav.doNotTrack,
appVersion: nav.appVersion,
winHeight: win.innerHeight,
winWidth: win.innerWidth,
location: location.href,
ipObject: ip,
date: date,
promise: ( win.Promise ) ? win.Promise : 'Unsupported',
time: date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
};
//silently fail if no callback is passed in
if ( customCallBack ) { customCallBack( obj ) }
else { console.log('ERROR: No callback function provided'); };
};
this.getCurrentPosition = function( nav, ip ) {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
},
self = this,
success = function( position ) {
self.returnInfo( position, ip );
},
error = function( error ) {
self.returnInfo( 'ERROR(' + error.code + '): ' + error.message, ip );
};
nav.geolocation.getCurrentPosition( success, error, options );
};
}