-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
61 lines (53 loc) · 1.67 KB
/
main.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
// Error handling function
function error() {
alert("Invalid domain or timestamp.")
}
//Function to make API requests
var sendRequest = function(url, callback) {
var xhr = new XMLHttpRequest(url);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
callback(JSON.parse(xhr.responseText));
}
else if (xhr.readyState == 4 && xhr.status == 404){
console.log(xhr.readyState, xhr.status);
error();
}
};
xhr.open("GET", url, true);
xhr.send();
};
//Object of functions holding each of the api requests
var api_calls = {
location: function(domain) {
var loc_url = "https://freegeoip.net/json/" + domain;
var location = {};
sendRequest(loc_url, function(xhr){
var data = xhr;
location.ip = data.ip;
location.city = data.city;
location.region = data.region_name;
location.country = data.country_name;
location.zip = data.zip_code;
location.timezone = data.time_zone;
location.coords = [data.latitude, data.longitude];
if (location.ip) {
return displayLocation(location);
}
});
},
archive: function(domain, timestamp) {
var time_url = "https://cors-anywhere.herokuapp.com/" + "http://archive.org/wayback/available?url=" + domain + "×tamp=" + timestamp;
var timeMachine = {};
sendRequest(time_url, function(xhr){
var data = xhr;
var dataArcSnap = data.archived_snapshots.closest;
timeMachine.status = dataArcSnap.status;
timeMachine.available = dataArcSnap.available;
timeMachine.url = dataArcSnap.url;
if (timeMachine.url) {
displayArchive(timeMachine);
}
});
}
};