blob: fae389190537750aa57e92773818c9dba6e526a4 (
plain)
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
|
GeoIp = {
cache: {},
pending: {},
get: function(ip, callback){
if (GeoIp.cache.hasOwnProperty(ip))
callback(GeoIp.cache[ip]);
else if (GeoIp.pending[ip])
GeoIp.pending[ip].push(callback);
else {
GeoIp.pending[ip] = [callback];
jQuery.ajax( {
url: "http://freegeoip.net/json/" + ip,
dataType: "json",
success: function(data) {
var res=[];
if (data.city) res.push(data.city);
if (data.region_name) res.push(data.region_name);
if (data.country_name) res.push(data.country_name);
data.fullName = res.join(", ");
GeoIp.cache[ip] = data;
var callbacks = GeoIp.pending[ip];
delete GeoIp.pending[ip];
for (var i=0; i<callbacks.length; i++)
callbacks[i].call(null, data);
},
error: function() {
var data = {ip:ip, fullName:""};
GeoIp.cache[ip] = data;
var callbacks = GeoIp.pending[ip];
delete GeoIp.pending[ip];
for (var i=0; i<callbacks.length; i++)
callbacks[i].call(null, data);
}
});
}
}
}
|