33 lines
1.9 KiB
JavaScript
33 lines
1.9 KiB
JavaScript
// ═══════════════════════════════════════════════════════
|
|
// URL Shortener
|
|
// ═══════════════════════════════════════════════════════
|
|
const urlHist = [];
|
|
async function shortenURL() {
|
|
const url = document.getElementById('urlInput').value.trim();
|
|
if (!url) return setStatus('urlStatus','error','Enter a URL.');
|
|
const d = await apiPost('/api/url/shorten', { url });
|
|
if (d.success) {
|
|
document.getElementById('shortUrlLink').href = d.shortUrl;
|
|
document.getElementById('shortUrlLink').textContent = d.shortUrl;
|
|
document.getElementById('urlResult').classList.add('visible');
|
|
setStatus('urlStatus','success','Shortened ✓');
|
|
urlHist.unshift({ short: d.shortUrl, original: url, time: new Date().toLocaleTimeString() });
|
|
renderUrlHistory();
|
|
} else setStatus('urlStatus','error', d.error);
|
|
}
|
|
function copyShortUrl() { copyText(document.getElementById('shortUrlLink').textContent); }
|
|
function renderUrlHistory() {
|
|
const c = document.getElementById('urlHistory');
|
|
if (!urlHist.length) { c.textContent = 'No links shortened yet.'; return; }
|
|
c.innerHTML = urlHist.slice(0,10).map(h => `
|
|
<div class="result-row">
|
|
<div style="overflow:hidden;flex:1;"><a href="${h.short}" target="_blank" style="color:var(--accent);font-family:var(--font-mono);font-size:0.8rem;text-decoration:none;">${h.short}</a>
|
|
<div style="font-size:0.7rem;color:var(--text-muted);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">${h.original}</div></div>
|
|
<span style="font-size:0.68rem;color:var(--text-muted);margin-left:12px;">${h.time}</span>
|
|
</div>`).join('');
|
|
}
|
|
|
|
// Keyboard shortcut
|
|
document.getElementById('urlInput').addEventListener('keydown', e => { if(e.key==='Enter') shortenURL(); });
|
|
|