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

101
Views
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 answers
Answer question

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