fix some bugs, refector the code and add the favicon

This commit is contained in:
sunwoo1524 2024-03-31 14:19:57 +09:00
parent 01d9d4f28f
commit 7b8484267c
4 changed files with 48 additions and 50 deletions

26
static/dQw4w9WgXcQ.js Normal file
View file

@ -0,0 +1,26 @@
// yeah it's a fucking easter egg :P
export const dQw4w9WgXcQ = {
check: (url) => {
try {
const urlObj = new URL(url);
let videoId = "";
if (urlObj.host.split(".").slice(-2)[0] === "youtube") {
if (urlObj.pathname.split("/")[1] === "watch") {
videoId = urlObj.searchParams.get("v");
} else {
return false;
}
} else if (urlObj.host === "youtu.be") {
videoId = urlObj.pathname.slice(1);
} else {
return false;
}
if (videoId ==="dQw4w9WgXcQ") return true;
return false;
} catch {
return false
}
}
};

BIN
static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -1,37 +1,22 @@
const url_input = document.getElementById("url"); const url_input = document.getElementById("url");
const to_shorten_btn = document.getElementById("to-shorten-btn"); const to_shorten_btn = document.getElementById("to-shorten-btn");
const result_container = document.getElementById("result-container");
const short_url = document.getElementById("short-url"); const short_url = document.getElementById("short-url");
const qr_code = document.getElementById("qr-code"); const qr_code = document.getElementById("qr-code");
const error_message = document.getElementById("error"); const error_message = document.getElementById("error");
const copy_message = document.getElementById("copied"); const copy_message = document.getElementById("copied");
to_shorten_btn.addEventListener("click", shorten);
addEventListener("keydown", (event) => {if(event.key == "Enter") shorten;});
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);
});
});
const shorten = async () => { const shorten = async () => {
const original_url = url_input.value const original_url = url_input.value;
// check if the url input is empty // check if the url input is empty
if (original_url.length === 0) { if (original_url.length === 0) {
return; return;
} }
// Easter egg: rick roll const { dQw4w9WgXcQ } = await import("./dQw4w9WgXcQ.js");
if (rickroll.check(original_url)){ if (dQw4w9WgXcQ.check(original_url)) {
window.location.replace("https://youtu.be/dQw4w9WgXcQ"); location.href = "https://youtu.be/dQw4w9WgXcQ";
return;
} }
// shorten the long url // shorten the long url
@ -52,7 +37,7 @@ const shorten = async () => {
error_message.style.display = "block"; error_message.style.display = "block";
setTimeout(() => error_message.style.display = "none", 5000); setTimeout(() => error_message.style.display = "none", 5000);
} else { } else {
// there is some problems in the server! // some error is occured in the server!
alert("Sorry, an error has occured in the server..."); alert("Sorry, an error has occured in the server...");
} }
@ -70,31 +55,18 @@ const shorten = async () => {
qr_code.alt = result; qr_code.alt = result;
} }
const rickroll = { to_shorten_btn.addEventListener("click", shorten);
check: (url)=>{ addEventListener("keydown", (event) => { if (event.key === "Enter") shorten() });
try {
const urlObj = new URL(url);
let videoId = "";
if (urlObj.host.split(".").slice(-2)[0] === "youtube") {
if (urlObj.pathname.split("/")[1] === "watch") {
videoId = urlObj.searchParams.get("v");
}
else {
return false;
}
}
else if (urlObj.host === "youtu.be") {
videoId = urlObj.pathname.slice(1);
}
else {
return false
}
if (videoId ==="dQw4w9WgXcQ") return true
return false
}
catch {
return false
}
}
}
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);
});
});

View file

@ -5,7 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{% endblock %}</title> <title>{% block title %}{% endblock %}</title>
<link rel="icon" href="favicon.ico" type="image/x-icon"> <link rel="icon" href="{{ url_for('static', path='/favicon.ico') }}">
<link rel="stylesheet" href="{{ url_for('static', path='/root.css') }}"> <link rel="stylesheet" href="{{ url_for('static', path='/root.css') }}">
{% endblock %} {% endblock %}
</head> </head>