Me gustaría poder tener 2 botones en mi panel de administración. Uno para copiar mi texto como "texto puro". Y un botón para copiar mi texto mientras agrego etiquetas html adicionales (Y
). Copio y pego mucho en mi trabajo, y esto realmente me ayudaría a trabajar un poco más rápido.
Digamos que tienes esta matriz:
let array = [{'head': 1, 'body': 'This is the body 1', 'content': 'This is the content 1'},{'head': 2, 'body': 'This is the body 2', 'content': 'This is the content 2'},{'head': 3, 'body': 'This is the body 3', 'content': 'This is the content 3'}]En esta matriz, me gustaría copiar/pegar el contenido (de la matriz anterior) de esta manera en un archivo de texto:
This is the body 1 This is the content 1 This is the body 2 This is the content 2 This is the body 3 This is the content 3 (El <br> no puede ser visible en el archivo de texto... debe ser un espacio en blanco que funcione en formato .txt, ¿podría funcionar este " "?)
El segundo botón también debe copiar el texto de la matriz, pero rodea el cuerpo y el contenido con las siguientes etiquetas:
<h2>This is the body 1</h2> <p>This is the content 1</p> <br> <h2>This is the body 2</h2> <p>This is the content 2</p> <br> <h2>This is the body 3</h2> <p>This is the content 3</p>En este momento, estoy aquí:
function copyToClipboard(){ let array = [{'head': 1, 'body': 'This is the body 1', 'content': 'This is the content 1'},{'head': 2, 'body': 'This is the body 2', 'content': 'This is the content 2'},{'head': 3, 'body': 'This is the body 3', 'content': 'This is the content 3'}] //Copy(clipboard) Text only var text = ""; //For Text only for (let i = 0; i < array.length; i++) { text += array[i]['body'] + " " + array[i]['content'] + " "; } //Copy(clipboard) Text with html tags for (let i = 0; i < array.length; i++) { text += "<h2>" + array[i]['body'] + "</h2>" + "<p>" + array[i]['content'] + "</p>"; } navigator.clipboard.writeText(text).then(() => { alert("Copied to clipboard"); console.log("Copied to clipboard"); }) }No estoy seguro de si voy en la dirección correcta.
Gracias por tu ayuda
Necesitarás tener 2 funciones una para cada botón.
let array = [{'head': 1, 'body': 'This is the body 1', 'content': 'This is the content 1'},{'head': 2, 'body': 'This is the body 2', 'content': 'This is the content 2'},{'head': 3, 'body': 'This is the body 3', 'content': 'This is the content 3'}] function copyAsText(){ //Copy(clipboard) Text only var text = ""; //For Text only for (let i = 0; i < array.length; i++) { text += array[i]['body'] + "\n" + array[i]['content'] + "\n"; } navigator.clipboard.writeText(text).then(() => { console.log("Copied to clipboard"); }) } function copyAsHTML(){ var text = ""; //Copy(clipboard) Text with html tags for (let i = 0; i < array.length; i++) { text += "<h2>" + array[i]['body'] + "</h2>" + "<br>" + "<p>" + array[i]['content'] + "</p><br>"; } navigator.clipboard.writeText(text).then(() => { console.log("Copied to clipboard"); }) }