Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8716773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:10:03+00:00 2026-06-13T06:10:03+00:00

In the background.html : chrome.tabs.query({active:true, currentWindow:true},function(tabs){ chrome.tabs.sendMessage(tabs[0].id,target.js); }); In the content.js : chrome.extension.onMessage.addListener(function(msg,sender,sendResponse){ if

  • 0

In the background.html:

chrome.tabs.query({active:true, currentWindow:true},function(tabs){
  chrome.tabs.sendMessage(tabs[0].id,"target.js");
});

In the content.js:

chrome.extension.onMessage.addListener(function(msg,sender,sendResponse){
  if (msg == "target.js"){
    extensionID = sender.id;
  }
});

However, it doesn’t work;

Uncaught TypeError: Cannot read property 'onMessage' of undefined 

How to make it right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:10:04+00:00Added an answer on June 13, 2026 at 6:10 am

    You said "content.js is a content script and it is injected to the current tab by another content script.".

    Herein lies your problem. Injected scripts are executed in the context of a page (as if they were created by the page itself), and therefore they have no access to any of the Chrome extension APIs.

    You could use a custom event or postMessage to communicate between the page and your content script, which in turn communicates with the background page.

    For instance, see this basic example:

        // Injected script script
        addEventListener('message', function(event) {
            if (event.data && event.data.extensionMessage) {
                alert(event.data.extensionMessage);
            }
        });
        // Content script which injects the script:
        chrome.extension.onMessage.addListener(function(message) {
            postMessage({extensionMessage: message}, '*');
        });
    

    I think that you want to use content.js as a real content script though, rather than an injected script. If you want to know the differences between injected scripts and content scripts, see Chrome extension code vs Content scripts vs Injected scripts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my background (background.html) page I have the following js: function capturePage(){ chrome.tabs.captureVisibleTab(null, function(img){
I've got the following in my background.html to sendRequest to contentscript... chrome.tabs.getSelected(null,function(tab) { chrome.tabs.sendRequest(tab.id,{req:func});
I am testing my Chrome extension by using the following codes: chrome.tabs.create({url: login_popup.html}, function(tab){
I have this in my background.html: chrome.management.onEnabled.addListener(function(ExtensionInfo info) { alert('123'); }); which gives me
I'm writing a Chrome extension, in my background.html page, it is injecting a js
I am trying to code a chrome extension, and I have a background.html with
I am trying to capture tab by function chrome.tabs.captureVisibleTab(..) on my chrome extension on
In my Google Chrome Extension, I have a Content Script (content.js) and a Background
How to change HTML background with JavaScript Function? I need some simple function to
$(#toggle).click(function(){ $(html).toggleClass(bg); }); html.bg { background: blue; } body { background: yellow; } <script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.