11

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?

dan-lee
  • 14,365
  • 5
  • 52
  • 77
user1753524
  • 121
  • 1
  • 1
  • 5

2 Answers2

14

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.

Jescanellas
  • 2,555
  • 2
  • 9
  • 20
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Thank you very much i will try later. There are two script in my extension . one is content.js one is target.js(which is a real content.js). And whether injects the content.js depends on the target.js.And i need get the extensionID in the content.js so i want to exchange the message between the background.html and content.js. – user1753524 Oct 22 '12 at 04:41
  • enlightened by what you said i use localStorage solve this problem....Thank you very much!!~~ – user1753524 Oct 22 '12 at 13:45
-1

It looks like you're trying to do some action to the current tab. Are you sure you need a message to do that? I'm assuming that background.html is your extension's background page and content.js is your content script. Now I skip the background.html page and simply run a javascript file for my background page. My manifest.json page would look something like this.

  "background": {
     "scripts": [ "js/background.js"]
  },

In that page, I add message listeners to my background page since that's the only way I know to interact with the background.js file. In your case however, it's the background page that doing the action therefore you could use the chrome.tab method to modify the current tab directly. Instead of onMessage, you may try something executeScript where the InjectDetails is the javascript or whatever you want to execute.

chrome.tabs.query({active:true, currentWindow:true},function(tabs){
         chrome.tabs.executeScript(tabs[0].id, InjectDetails);
});

See more on that HERE.

Brantley Blanchard
  • 1,208
  • 3
  • 14
  • 23
  • In fact, i only want to get my extension's id in the content.js.But it seems no chromeAPI is allowed in the content.js.So i try this method,but it fails..... content.js is a content script and it is injected to the current tab by another content script i don't know whether it matters.... – user1753524 Oct 21 '12 at 12:08