2024-02-19 12:21:12 +09:00
|
|
|
const url_input = document.getElementById("url");
|
|
|
|
const to_shorten_btn = document.getElementById("to-shorten-btn");
|
|
|
|
const short_url = document.getElementById("short-url");
|
|
|
|
const qr_code = document.getElementById("qr-code");
|
|
|
|
const error_message = document.getElementById("error");
|
|
|
|
const copy_message = document.getElementById("copied");
|
|
|
|
|
2024-03-30 18:12:46 +09:00
|
|
|
const shorten = async () => {
|
2024-03-31 14:19:57 +09:00
|
|
|
const original_url = url_input.value;
|
|
|
|
|
2024-02-19 12:21:12 +09:00
|
|
|
// check if the url input is empty
|
|
|
|
if (original_url.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-31 14:19:57 +09:00
|
|
|
const { dQw4w9WgXcQ } = await import("./dQw4w9WgXcQ.js");
|
|
|
|
if (dQw4w9WgXcQ.check(original_url)) {
|
|
|
|
location.href = "https://youtu.be/dQw4w9WgXcQ";
|
|
|
|
return;
|
2024-02-19 12:21:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// shorten the long url
|
|
|
|
const response = await fetch("/api/urls", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
url: original_url
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// check if there is problems
|
|
|
|
if (!response.ok) {
|
|
|
|
if (response.status === 400) {
|
|
|
|
// the url is invalid
|
|
|
|
error_message.style.display = "block";
|
|
|
|
setTimeout(() => error_message.style.display = "none", 5000);
|
|
|
|
} else {
|
2024-03-31 14:19:57 +09:00
|
|
|
// some error is occured in the server!
|
2024-02-19 12:21:12 +09:00
|
|
|
alert("Sorry, an error has occured in the server...");
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await response.json();
|
2024-05-05 16:03:06 +09:00
|
|
|
const result = `${host}/${data.key}`;
|
2024-02-19 12:21:12 +09:00
|
|
|
|
|
|
|
// show the short url
|
|
|
|
short_url.innerText = result;
|
|
|
|
|
|
|
|
// get the qr code of the short url and show it
|
|
|
|
qr_code.src = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${result}`;
|
|
|
|
qr_code.alt = result;
|
2024-03-30 18:12:46 +09:00
|
|
|
}
|
2024-02-19 12:21:12 +09:00
|
|
|
|
2024-03-31 14:19:57 +09:00
|
|
|
to_shorten_btn.addEventListener("click", shorten);
|
|
|
|
addEventListener("keydown", (event) => { if (event.key === "Enter") shorten() });
|
2024-02-19 12:21:12 +09:00
|
|
|
|
2024-03-31 14:19:57 +09:00
|
|
|
short_url.addEventListener("click", event => {
|
|
|
|
// copy the short url to clipboard
|
|
|
|
navigator.clipboard.writeText(event.target.innerText)
|
|
|
|
.then(() => {
|
|
|
|
// show a message
|
|
|
|
copy_message.style.opacity = "1";
|
|
|
|
setTimeout(() => copy_message.style.opacity = "0", 3000);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
alert("An error has occurred...\n" + error);
|
|
|
|
});
|
|
|
|
});
|