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

113
Visualizações
Add event listener to a button on a Page Chrome Extension

In content.js, I am trying to add an event listener to a button on the page but I keep getting this error:

Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

I have tried to put it in document.addEventListener('DOMContentLoaded',afterDOMLoaded); but DOMContentLoaded never fires in the content.js script. How can I add an event listener to a button on the page?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should show how your content.js is executed.

Maybe, the page has already DOMContentLoaded when your script is inejcted. The injection timing can be specified in manifest file. If your manifest file is the following, your content.js is injected immediately after the window.onload event fires, that is after DOMContentLoaded. Refer https://developer.chrome.com/docs/extensions/mv3/content_scripts/.

"content_scripts": [
{
  "matches": ["https://*/*"],
  "run_at": "document_idle",
  "js": ["content.js"]
}

If your manifest is like the above, you do not need event listener, just write the code in content.js script instead.

about 4 years ago · Juan Pablo Isaza Relatório
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