funcionamiento de copiar
This commit is contained in:
@@ -106,11 +106,29 @@ btnClear.addEventListener('click', async () => {
|
||||
});
|
||||
|
||||
btnCopy.addEventListener('click', async () => {
|
||||
const text = JSON.stringify(history, null, 2);
|
||||
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');
|
||||
} catch (e) {
|
||||
alert('No se pudo copiar: ' + e.message);
|
||||
alert('No se pudo copiar: ' + (e && e.message ? e.message : e));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user