frontend draft

This commit is contained in:
Thomas Schleicher 2024-12-16 12:46:52 +01:00
parent acb7221e34
commit f61be671d6
2 changed files with 78 additions and 0 deletions

34
frontend/index.js Normal file
View file

@ -0,0 +1,34 @@
document.getElementById("urlInput").addEventListener("input", function() {
const urlInput = document.getElementById("urlInput");
const shortenButton = document.getElementById("shortenButton");
const urlPattern = /^(https?:\/\/)?([\w\d-]+\.)+[a-z]{2,6}(\/[\w\d-]*)*\/?$/i;
const isValid = urlPattern.test(urlInput.value);
if (isValid) {
urlInput.style.borderColor = "green";
shortenButton.disabled = false;
} else {
urlInput.style.borderColor = "red";
shortenButton.disabled = true;
}
});
document.getElementById("shortenButton").addEventListener("click", async function() {
const urlInput = document.getElementById("urlInput").value;
const outputContainer = document.getElementById("output-container");
if (urlInput) {
// const shortenedUrl = urlInput + "-short"; // only for testing
const shortenedUrl = await fetch("0.0.0.0:8080/", {
method: "POST",
headers: {
"Content-Type": "text/plain",
},
body: urlInput,
});
outputContainer.innerHTML = `<p>Shortened URL: <a href="${shortenedUrl}" target="_blank">${shortenedUrl}</a></p>`;
}
});