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

153
Views
using index.html(mainstream webpage, not a popup.html) how to send data from content.js to background.js only after onClick method?

The whole point is to send some data from HTML page(Not a popup window) to content.js page when fired a onclick and then send it to background.js file to perform some function with the data but i'm facing some errors to get it.

index.html

<input type="text" id='fullname' />
<button onclick="sendData()">Submit</button>

Content.js

chrome.runtime.sendMessage() method works fine when inside document.onLoad() but having an issue calling it inside a normal function

function sendData(){
    var fullName = document.getElementById('fullname').value;
    chrome.runtime.sendMessage({ "message": fullName }
    ,(res)=>console.log(res))
}

Background.js

chrome.runtime.onMessage.addListener(receiver);

function receiver(data, sender, sendResponse) {
  if (data.message !== undefined) {
    chrome.pageAction.show(sender.tab.id);
    sendResponse(`${data.message} is submitted as your fullname`)
  }
}

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

0

Chrome Extensions do not allow you to have inline JavaScript (documentation).

Instead, assign an id to your button <button id="submitButton">Submit</button> and and use addEventListener() to bind the sendData() function as an event.

Content.js

document.addEventListener('DOMContentLoaded', function() {
    const button = document.getElementById('submitButton');
    button.addEventListener('click', sendData);
});

function sendData(){
    var fullName = document.getElementById('fullname').value;
    chrome.runtime.sendMessage({ "message": fullName }
    ,(res)=>console.log(res))
}
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!