34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
function copyUrlToClipboard() {
|
|
const urlInput = document.getElementById("url-input");
|
|
urlInput.select();
|
|
urlInput.setSelectionRange(0, 99999);
|
|
navigator.clipboard.writeText(urlInput.value);
|
|
}
|
|
|
|
function resetLinkGeneration() {
|
|
}
|
|
|
|
async function shortenUrl() {
|
|
const urlInput = document.getElementById("url-input");
|
|
const btn = document.getElementById("submit-btn");
|
|
// btn.enab
|
|
const response = await fetch("/api/v1/urls", {
|
|
method: 'PUT',
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ url: urlInput.value, username: "anonymous" })
|
|
});
|
|
const data = await response.json();
|
|
const resultElement = document.getElementById("result");
|
|
|
|
if (response.ok) {
|
|
resultElement.textContent = ''
|
|
urlInput.value = data.shortenedUrl;
|
|
btn.innerText = "📋 Copy to Clipboard!";
|
|
btn.onclick = copyUrlToClipboard;
|
|
} else {
|
|
resultElement.textContent = `Error: ${data.detail}`
|
|
resultElement.style.color = "red";
|
|
btn.value = "Shorten Url";
|
|
}
|
|
}
|