-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
161 lines (140 loc) · 4.96 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rusly - URL Shortener</title>
<link rel="icon" type="image/x-icon" href="favicon.png">
<link rel="stylesheet" href="index.css" />
</head>
<body>
<header>
<h1>Rusly</h1>
<p class="tagline">
A URL Shortener built by <a href="https://links.wilfredalmeida.com" target="_blank" style="text-decoration:none; color: white;">Wilfred Almeida</a> using Rust Rocket and SQLite
</p>
<nav>
<ul>
<li><a href="https://blog.wilfredalmeida.com" target="_blank">Blog</a></li>
<li><a href="https://github.com/WilfredAlmeida/rusly" target="_blank">GitHub</a></li>
</ul>
</nav>
</header>
<main>
<form id="submitform">
<label for="url">Enter URL:</label>
<input
type="text"
id="url"
name="url"
placeholder="https://www.example.com"
/>
<div id="toggle-wrapper">
<label for="custom-url-toggle">Custom URL:</label>
<input type="checkbox" id="custom-url-toggle" />
</div>
<div id="custom-url-wrapper">
<label for="custom-url">Enter custom URL:</label>
<input
type="text"
id="custom-url"
name="custom-url"
placeholder="short-url"
/>
</div>
<button type="submit" class="btn">Shorten URL</button>
</form>
<div class="loading">
<img width="100px" height="100px" src="crab.gif" alt="Loading..." />
</div>
<div class="result">
<p>Shortened URL:</p>
<input type="text" id="shortened-url" name="shortened-url" readonly width="100%"/>
<button type="button" class="btn" onclick="copyToClipboard()">
Copy
</button>
</div>
</main>
<div class="footer">
<p class="made-with-love">Made with 💖 by <a href="https://links.wilfredalmeida.com" target="_blank" style="text-decoration:none; color: white;">Wilfred Almeida</a></p>
</div>
<script type="text/javascript">
const form = document.getElementById("submitform");
const urlInput = document.querySelector("#url");
const resultDiv = document.querySelector(".result");
const shortenedUrlInput = document.querySelector("#shortened-url");
const loadingDiv = document.querySelector(".loading");
const customUrlToggle = document.getElementById("custom-url-toggle");
const customUrlWrapper = document.getElementById("custom-url-wrapper");
customUrlWrapper.style.display = "none";
customUrlToggle.addEventListener("change", function () {
if (this.checked) {
customUrlWrapper.style.display = "block";
} else {
customUrlWrapper.style.display = "none";
}
});
function copyToClipboard() {
navigator.clipboard.writeText(shortenedUrlInput.value);
}
window.onload = () => {
form.addEventListener("submit", async (event) => {
event.preventDefault();
const url = urlInput.value;
loadingDiv.style.display = "block";
if (!url || url.trim().length === 0) {
alert("Please enter a valid URL.");
loadingDiv.style.display = "none";
return;
}
const customUrl = customUrlToggle.checked
? customUrlWrapper.querySelector("#custom-url").value
: null;
if (
customUrl !== null &&
(customUrl.length != 7 || !isCustomLinkValid(customUrl))
) {
alert("Custom url should be 7 alphabets. Hyphens (-) are allowed");
loadingDiv.style.display = "none";
return;
}
try {
// Call API
var raw = JSON.stringify({
url_to_shorten: url,
custom_link: customUrl,
});
var requestOptions = {
method: "POST",
headers:{
origin: "https://wilfredalmeida.com",
},
body: raw,
redirect: "follow",
};
const response = await fetch(
"https://rusly-production.up.railway.app/v1/shorten",
requestOptions
);
if (!response.ok) {
alert(response.body);
throw new Error(`HTTP error! status: ${response.status}`);
}
const json = await response.json();
const shortenedUrl = json.shortened_url;
// Show result
shortenedUrlInput.value = shortenedUrl;
resultDiv.style.display = "block";
loadingDiv.style.display = "none";
} catch (e) {
console.error("Error:", e);
}
});
};
function isCustomLinkValid(murl) {
const pattern = RegExp("^[a-zA-Z-]+");
return pattern.test(murl);
}
</script>
</body>
</html>