Copia qualquer texto para o clipboard com fallback para navegadores mais antigos.
async function copiarTexto(texto) {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(texto);
return true;
}
// Fallback
const el = document.createElement('textarea');
el.value = texto;
el.style.cssText = 'position:fixed;opacity:0;pointer-events:none';
document.body.appendChild(el);
el.select();
const ok = document.execCommand('copy');
document.body.removeChild(el);
return ok;
}
// Uso com feedback visual:
document.querySelectorAll('[data-copy]').forEach(btn => {
btn.addEventListener('click', async () => {
await copiarTexto(btn.dataset.copy);
btn.textContent = 'Copiado!';
setTimeout(() => btn.textContent = 'Copiar', 1500);
});
});