-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
128 lines (113 loc) · 3.27 KB
/
index.html
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>URL Shortener</title>
<style>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: rgb(42, 42, 42);
color: white;
}
.header {
margin-top: 30px;
text-align: center;
}
#myForm {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin-top: 50px;
}
#url {
min-width: 60vw;
padding: 5px;
margin: 3px;
margin-bottom: 10px;
}
#btn {
padding: 10px;
background-color: green;
border: none;
color: white;
cursor: pointer;
font-size: 13px;
}
#shortenedURL {
margin-top: 50px;
font-size: 16px;
}
#generatedURL {
color: yellowgreen;
}
.loader {
border: 10px solid #f3f3f3;
/* Light grey */
border-top: 10px solid #3498db;
/* Blue */
border-radius: 50%;
width: 80px;
height: 80px;
animation: spin 2s linear infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<h1 class="header">
URL Shortener
</h1>
<form id='myForm'>
<input type='text' name='url' id='url' required>
<button id='btn' type='submit'>Short Url</button>
<p id='shortenedURL'> </p>
</form>
</body>
<script>
var form = document.getElementById("myForm");
const handleSubmit = (e) => {
e.preventDefault();
var url = document.getElementById('url').value;
var response = document.getElementById('shortenedURL');
response.innerHTML = '<div class="loader"></div>';
fetch('/shorturl', {
method: 'POST',
body: JSON.stringify({ 'url': url }),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
})
.then(res => {
if (res.ok) {
return res.json()
}
else {
response.innerHTML = res.statusText;
}
})
.then((data) => {
response.innerHTML = `Shortened URL: <a id='generatedURL' target='_blank' href='${window.location.protocol}//${window.location.hostname}:${window.location.port}/${data.shortened_url}'>${window.location.protocol}//${window.location.hostname}:${window.location.port}/${data.shortened_url}</a>`;
})
.catch(err => {
response.innerHTML = 'ERROR: Unable to access server';
console.log(err)
})
}
form.addEventListener('submit', handleSubmit);
</script>
</html>