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

157
Views
Send message from extension popup script to content script - JS

I'm making a web extension and I want a message to be sent to my content script when the following button of my popup.html is clicked.

<button class="button" type="button" id="stop">Stop</button>
<script src="stopCount.js"></script>

Here's stopCount.js :

let button = document.getElementById('stop');

button.addEventListener('click', function(event){
if (button.textContent == "Stop") {
    button.textContent = "Resume";
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
       chrome.tabs.sendMessage(
           tabs[0].id,
           {msg: "stop"});
    });
}

else if (button.textContent == "Resume") {     
    button.textContent = "Stop";
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(
            tabs[0].id,
            {msg: "resume"});
    });
  }    
})

And here's the part of my content script that intercepts messages :

chrome.runtime.onMessage.addListener(function(request, sender, response) {
if (request.msg == "stop") {
    observer.disconnect();
  }
else if (request.msg == "resume") {
    observer = new MutationObserver(callback);
    observer.observe(targetNode, config);
  }
})

My problem is the part of my content script described just above is never reached, even if I click on the button.

Do you know what is the problem ?

Thanks in advance,

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!