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

158
Visualizações
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
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