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

186
Views
Error with Chrome Extension sending message from background to content script

I'm learning about Chrome Extensions and I want to send a message from the background to a content script. I'm trying to do the simplest example, but I keep getting the following error:

Error in event handler: TypeError: Cannot read properties of undefined (reading 'id') at sendMessageBackgroundToScript (chrome-extension://ibghimmhcpdahopejafegpkgfcijmcin/event.js:4:35)

This is the code:

manifest.json:

{
    "name": "Send Messages",
    "description": "An example of how to send messages from background to script",
    "version": "1.0",
    "manifest_version": 3,
    "background": {
        "service_worker": "event.js"
    },
    "action": {
        "default_title": "Send Message",
        "default_icon": "images/icon32.png"
    },
    "permissions": [
        "scripting"
    ],
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["script.js"]
        }
    ]

}

event.js:

chrome.action.onClicked.addListener(sendMessageBackgroundToScript);

function sendMessageBackgroundToScript(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {text: "This is a message"})
}

script.js:

chrome.runtime.onMessage.addListener(gotMessage);

function gotMessage(message, sender, sendResponse) {
  console.log(message.text);
}
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!