Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Copiar al portapapeles en la extensión de Chrome V3

Estoy desarrollando una extensión de Chrome V3. Quiero copiar contenido al portapapeles en mi archivo JS.
El manifest.json como se muestra a continuación,

 "background" :{ "service_worker" :"eventPage.js" }, "permissions" : [ "contextMenus", "clipboardWrite" ]

He probado 2 soluciones para la función de copia.

Solución 1:

 const el = document.createElement('textarea'); el.value = str; el.setAttribute('readonly', ''); el.style.position = 'absolute'; el.style.left = '-9999px'; document.body.appendChild(el); el.select(); document.execCommand('copy'); document.body.removeChild(el);

El resultado:

 Error in event handler: ReferenceError: document is not defined at copyToClipboard

Solución 2:

 navigator.clipboard.writeText(str);

El resultado:

 Error in event handler: TypeError: Cannot read properties of undefined (reading 'writeText')

La extensión de Chrome se ejecuta como un trabajador de servicio. Entonces parece que no puedo acceder al documento DOM y no tengo la concesión de writeText. ¿Alguien tiene otra sugerencia?

Gracias.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Seguiré la excelente sugerencia que te dio wOxxOm, desarrollándola en un ejemplo concreto. Lo que quiere hacer es tener un ContentScript.js que se ejecute en cualquier pestaña activa con una página web, ya que no puede acceder al DOM desde backGround.js, y luego enviar un mensaje a este script, desde donde copiaría al portapapeles.

manifiesto.json

 "background" :{ "service_worker" :"eventPage.js" }, "permissions" : [ "contextMenus", "clipboardWrite" ], "content_scripts": [ // this is what you need to add { "matches": [ "<all_urls>" ], "js": ["content.js"] } ],

Desde background.js, enviaría un mensaje, que se manejará en ContentScript.js

fondo.js

 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, { message: "copyText", textToCopy: "some text" }, function(response) {}) })

En contentScript.js, captaría el mensaje y lo copiaría en el portapapeles.

contenido.js

 chrome.runtime.onMessage.addListener( // this is the message listener function(request, sender, sendResponse) { if (request.message === "copyText") copyToTheClipboard(request.textToCopy); } ); async function copyToTheClipboard(textToCopy){ const el = document.createElement('textarea'); el.value = textToCopy; el.setAttribute('readonly', ''); el.style.position = 'absolute'; el.style.left = '-9999px'; document.body.appendChild(el); el.select(); document.execCommand('copy'); document.body.removeChild(el); }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!