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

175
Views
¿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
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!