Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
¿Cómo copiar texto y enviarlo a un archivo HTML usando sendMessage?

Lo que quiero hacer:

Estoy tratando de construir una extensión de cromo. La extensión será-

  1. Al hacer clic en el icono una vez, seguirá funcionando.
  2. Después de eso, en cualquier página/pestaña, el usuario podrá seleccionar algún texto y usar una combinación de teclas (por ejemplo, Shift+a) para enviar ese texto a una página HTML, digamos text.html .
  3. Si vuelvo a hacer clic en el icono, la extensión dejará de funcionar.

Tenga en cuenta que text.html es una página fija, todo el texto se acumulará en text.html .

Mi intento:

  1. Ejecute un JavaScript en segundo plano para recopilar texto, luego envíelo usando sendMessage , algo como:

contenido.js

 function getSelectionText() { var text = ""; if (window.getSelection) { text = window.getSelection().toString(); } else if (document.selection && document.selection.type != "Control") { text = document.selection.createRange().text; } return text; } function doc_keyUp(e) { // this would test for whichever key is 40 (down arrow) and the ctrl key at the same time if (e.key === 'a') { tx_s=getSelectionText(); //console.log(tx_s); chrome.runtime.sendMessage({greeting:tx_s},function(response) {console.log(response.farewell);}); }// if (e.key === 'q') } // doc_keyUp(e) // register the handler document.addEventListener('keyup', doc_keyUp, false);

recibe texto por el siguiente código:

fondo.js

 chrome.runtime.onMessage.addListener( function(request,sender,sendResponse){ console.log(sender.tab? "from a content script"+sender.tab.url : "from the extension"); //if (request.greeting==="hello") sendResponse({farewell:request.greeting}); console.log(request.greeting); //window.greeting[0] = request.greeting; }) chrome.action.onClicked.addListener(function (tab) { chrome.tabs.create({url: 'popup.html'}) })
  1. Luego obtenga el texto en el archivo HTML, obtuve la idea de "Cómo hacer extensiones de Chrome", Kyle Robinson Young (haga clic aquí) , pero el código no funciona debido a un problema de versión de manifiesto, también el código obtiene el error window.bears={}; (no permite la opción de ventana).

¿Cómo puedo transferir el texto de varias páginas a una acumulación y ver en un html? Gracias.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda