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

165
Views
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
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!