-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
108 lines (85 loc) · 3.08 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var resultsContainer = document.getElementById("results");
var btn = document.getElementById("submit_btn");
var ip = "";
var loading_circle = document.getElementById("loading_circle")
function callback (response) {
console.log(response);
}
function ip2int(ip) {
return ip.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0;
}
$.ajax({
type: 'GET',
dataType:'json',
async:false,
url:'https://httpbin.org/ip',
success: function(responseData) {
ip = ip2int(responseData.origin);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
console.log('Error');
}
})
window.onload = function() {
loading_circle.style.visibility = "hidden";
var url = 'https://beautiful-code.glasstea.repl.co/' + ip;
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET',url)
ourRequest.onload = function() {
console.log('hello!')
console.log(ourRequest.responseText);
};
ourRequest.send();
return 'hi'
};
btn.addEventListener("click",function() {
var input_url = document.getElementById('input_link').value;
if (input_url == ''){
add_error('Please input a url');
return;
}
if (input_url.includes('.com/') == false && input_url.includes('.net/') == false) {
add_error('Please input a valid url');
return;
}
if (input_url.includes('dynamic')) {
add_error('That is a dynamic link. Please enter a static link.');
return;
}
loading_circle.style.visibility = "visible";
if (input_url.includes('.com/') == true) {
if (input_url.includes('?o=')) {
var start = input_url.indexOf('.com/') + 5;
input_url = input_url.substring(start, input_url.indexOf('?o='));
} else {
input_url = input_url.substring(input_url.indexOf('.com/') + 5, input_url.length)
}
}
if (input_url.includes('.net/') == true) {
if (input_url.includes('?o=')) {
var start = input_url.indexOf('.net/') + 5;
input_url = input_url.substring(static, input_url.indexOf('?o='));
} else {
input_url = input_url.substring(input_url.indexOf('.net/') + 5, input_url.length)
}
}
console.log(input_url)
var url = 'https://beautiful-code.glasstea.repl.co/' + input_url + '/' + ip;
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET',url)
ourRequest.onload = function() {
loading_circle.style.visibility = "hidden";
var ourData = JSON.parse(ourRequest.responseText);
add_link(ourData);
};
ourRequest.send();
});
function add_error(error_code) {
resultsContainer.insertAdjacentHTML('afterbegin','<p>' + error_code+ '</p>');
console.log(error_code)
}
function add_link(data) {
var new_link = data.new_link;
resultsContainer.insertAdjacentHTML('afterbegin','<a class = "link" href="' + new_link + '">' + new_link + '</a>');
console.log(data)
}