(!longUrl) { alert("Enter a URL first!"); return; }
try {
// Use tinyurl API
const apiUrl = customCode
? `https://tinyurl.com/api-create.php?url=${encodeURIComponent(longUrl)}&alias=${encodeURIComponent(customCode)}`
: `https://tinyurl.com/api-create.php?url=${encodeURIComponent(longUrl)}`;
const res = await fetch(apiUrl);
const shortUrl = await res.text();
document.getElementById('result').innerHTML = `
✅ Your short URL:
${shortUrl}
`;
} catch (err) {
alert("Something went wrong: " + err.message);
}
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => alert("Copied: " + text));
}