document.addEventListener('DOMContentLoaded', () => { /* ── Create-paste page ── */ const pasteForm = document.getElementById('pasteForm'); const resultBox = document.getElementById('result'); const pasteText = document.getElementById('pasteText'); const charCount = document.getElementById('charCount'); const submitBtn = document.getElementById('submitBtn'); if (pasteText && charCount) { const update = () => { charCount.textContent = pasteText.value.length.toLocaleString(); pasteText.style.height = 'auto'; pasteText.style.height = Math.max(280, pasteText.scrollHeight) + 'px'; }; pasteText.addEventListener('input', update); update(); } if (pasteForm) { pasteForm.addEventListener('submit', async (e) => { e.preventDefault(); if (!pasteText || pasteText.value.trim() === '') { pasteText.focus(); pasteText.style.borderColor = 'var(--error)'; setTimeout(() => pasteText.style.borderColor = '', 1200); return; } submitBtn.classList.add('loading'); submitBtn.querySelector('span').textContent = 'Creating...'; try { const res = await fetch('/index.php?action=save', { method: 'POST', body: new FormData(pasteForm), }); const data = await res.json(); if (!data.success) { showError(data.message || 'Something went wrong.'); return; } const linkInput = document.getElementById('pasteLink'); const viewLink = document.getElementById('viewLink'); linkInput.value = data.url; viewLink.href = data.url; resultBox.removeAttribute('hidden'); resultBox.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); pasteText.value = ''; if (charCount) charCount.textContent = '0'; } catch { showError('Network error. Please try again.'); } finally { submitBtn.classList.remove('loading'); submitBtn.querySelector('span').textContent = 'Create Secure Link'; } }); function showError(msg) { resultBox.removeAttribute('hidden'); resultBox.style.borderLeftColor = 'var(--error)'; resultBox.innerHTML = `
${msg}
`; } } /* ── Copy link button (result box) ── */ document.addEventListener('click', (e) => { const btn = e.target.closest('#copyLinkBtn'); if (!btn) return; const val = document.getElementById('pasteLink')?.value; if (!val) return; copyToClipboard(val, btn, 'Copied!', 'Copy'); }); /* ── View-paste page: copy content ── */ const copyBtn = document.getElementById('copyBtn'); if (copyBtn) { copyBtn.addEventListener('click', () => { const content = document.getElementById('pasteContent')?.innerText; if (!content) return; copyToClipboard(content, copyBtn, 'Copied!', 'Copy'); }); } /* ── Shared helper ── */ function copyToClipboard(text, btn, successLabel, defaultLabel) { navigator.clipboard.writeText(text).then(() => { const span = btn.querySelector('span') || btn; btn.classList.add('copied'); span.textContent = successLabel; setTimeout(() => { btn.classList.remove('copied'); span.textContent = defaultLabel; }, 2000); }); } });