document.addEventListener('DOMContentLoaded', function () { const shortenForm = document.getElementById('shortenForm'); const urlInput = document.getElementById('urlInput'); const resultDiv = document.getElementById('result'); const shortUrlSpan = document.getElementById('short-url'); const copyBtn = document.getElementById('copyBtn'); shortenForm.addEventListener('submit', function (e) { e.preventDefault(); const longUrl = urlInput.value.trim(); // Simple URL validation if (!isValidHttpUrl(longUrl)) { alert('Please enter a valid URL (e.g., https://example.com)'); urlInput.focus(); return; } // --- Dummy Shortener Logic --- // In a real app, this would be an API call. const randomString = Math.random().toString(36).substring(2, 8); const shortUrl = `yourdomain.com/${randomString}`; // --- End Dummy Logic --- shortUrlSpan.textContent = shortUrl; resultDiv.style.display = 'block'; }); copyBtn.addEventListener('click', function() { navigator.clipboard.writeText(shortUrlSpan.textContent).then(() => { copyBtn.textContent = 'Copied!'; setTimeout(() => { copyBtn.textContent = 'Copy'; }, 2000); }).catch(err => { console.error('Failed to copy: ', err); }); }); function isValidHttpUrl(string) { let url; try { url = new URL(string); } catch (_) { return false; } return url.protocol === "http:" || url.protocol === "https:"; } });