document.addEventListener('DOMContentLoaded', () => {
const blocks = document.querySelectorAll('.lp-copy-block');
if (!blocks.length || !navigator.clipboard) return;
blocks.forEach(block => {
const codeEl = block.querySelector('code, .lp-copy-target');
if (!codeEl) return;
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'lp-copy-btn';
btn.setAttribute('aria-label', '複製內容');
btn.innerHTML = `
`;
block.appendChild(btn);
btn.addEventListener('click', async () => {
try {
const text = (codeEl.innerText || codeEl.textContent || '').trim();
await navigator.clipboard.writeText(text);
btn.classList.add('copied');
setTimeout(() => btn.classList.remove('copied'), 800);
} catch (err) {
console.error('Copy failed', err);
}
});
});
});