Last change
on this file since 22664 was
22664,
checked in by rvelices, 11 years ago
|
feature 2885: Add geoip info in the stats / history page
|
File size:
1.0 KB
|
Line | |
---|
1 | |
---|
2 | GeoIp = { |
---|
3 | cache: {}, |
---|
4 | pending: {}, |
---|
5 | |
---|
6 | get: function(ip, callback){ |
---|
7 | if (GeoIp.cache.hasOwnProperty(ip)) |
---|
8 | callback(GeoIp.cache[ip]); |
---|
9 | else if (GeoIp.pending[ip]) |
---|
10 | GeoIp.pending[ip].push(callback); |
---|
11 | else { |
---|
12 | GeoIp.pending[ip] = [callback]; |
---|
13 | jQuery.ajax( { |
---|
14 | url: "http://freegeoip.net/json/" + ip, |
---|
15 | dataType: "json", |
---|
16 | success: function(data) { |
---|
17 | var res=[]; |
---|
18 | if (data.city) res.push(data.city); |
---|
19 | if (data.region_name) res.push(data.region_name); |
---|
20 | if (data.country_name) res.push(data.country_name); |
---|
21 | data.fullName = res.join(", "); |
---|
22 | |
---|
23 | GeoIp.cache[ip] = data; |
---|
24 | var callbacks = GeoIp.pending[ip]; |
---|
25 | delete GeoIp.pending[ip]; |
---|
26 | for (var i=0; i<callbacks.length; i++) |
---|
27 | callbacks[i].call(null, data); |
---|
28 | }, |
---|
29 | |
---|
30 | error: function() { |
---|
31 | var data = {ip:ip, fullName:""}; |
---|
32 | |
---|
33 | GeoIp.cache[ip] = data; |
---|
34 | var callbacks = GeoIp.pending[ip]; |
---|
35 | delete GeoIp.pending[ip]; |
---|
36 | for (var i=0; i<callbacks.length; i++) |
---|
37 | callbacks[i].call(null, data); |
---|
38 | } |
---|
39 | }); |
---|
40 | } |
---|
41 | } |
---|
42 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.