funcionamiento de copiar

This commit is contained in:
2025-09-24 15:26:28 -06:00
parent dc437f50d5
commit 4f409cb4ec

View File

@@ -106,11 +106,29 @@ btnClear.addEventListener('click', async () => {
}); });
btnCopy.addEventListener('click', async () => { btnCopy.addEventListener('click', async () => {
const text = JSON.stringify(history, null, 2);
try { try {
await navigator.clipboard.writeText(JSON.stringify(history, null, 2)); async function copyText(t) {
if (navigator.clipboard && navigator.clipboard.writeText) {
return navigator.clipboard.writeText(t);
}
const ta = document.createElement('textarea');
ta.value = t;
ta.style.position = 'fixed';
ta.style.opacity = '0';
ta.setAttribute('readonly', '');
document.body.appendChild(ta);
ta.focus();
ta.select();
ta.setSelectionRange(0, ta.value.length);
const ok = document.execCommand('copy');
document.body.removeChild(ta);
if (!ok) throw new Error('copy command failed');
}
await copyText(text);
alert('Copiado al portapapeles'); alert('Copiado al portapapeles');
} catch (e) { } catch (e) {
alert('No se pudo copiar: ' + e.message); alert('No se pudo copiar: ' + (e && e.message ? e.message : e));
} }
}); });