Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
How to copy text and send it to a HTML file using sendMessage?

What I want to do:

I am trying to build a chrome-extension. The extension will-

  1. By clicking the icon once, it will keep running.
  2. After that, on any page/tab, the user would be able to select some text and use a key combination (e.g. Shift+a) to send that text to an HTML page, say text.html.
  3. If I click the icon again, the extension will stop working.

Note that text.html is a fixed page, all text will be accumulated on text.html.

My Attempt:

  1. Run a JavaScript on background to collect text, then send it by using sendMessage, something like :

content.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);

it receives text by the below code:

background.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. Then get the textto the HTML file, I got the idea from "How To Make Chrome Extensions", Kyle Robinson Young (click here), but the code is not working due to manifes version problem, also the code gets error window.bears={}; (does not allow window option).

How can I transfer the text from various page to a accumlate and view on a html? Thanks.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda