1

I've been struggling for days on this, my extension's content script seems to be missing permissions or something, I've searched through the API docs and found nothing.

The messaging works if I send a message FROM the content page, but not TO the content page with the following code:

From the background page:

var messageCallback = function (e) {
    var nodeMessage = JSON.parse(e.data);

    switch (nodeMessage.ExecutionType) {
        case 0:
            chrome.tabs.create({ url: nodeMessage.Url }, function (tab) {

                //injects injected.js NOT messages.js
                injectCode(tab.id);

                chrome.tabs.sendMessage(tab.id,nodeMessage);
            });
        break;
//some other switch cases...

From the content script:

chrome.runtime.onMessage.addListener(function (message) {
    console.log('Message received');

    var event = new CustomEvent("foo_receive", {
        detail: message,
        bubbles: true
    });

    console.log('Event sent');
    document.dispatchEvent(event);
});

My manifest looks like this:

{
  "manifest_version": 2,

  "name": "extesnion_name",
  "short_name": "thing",
  "description": "long sentence",
  "version": "1.0.0",
  "icons": { "16": "icon16.png",
             "48": "icon48.png",
             "128": "icon128.png" },
  "permissions": ["background", "tabs", "<all_urls>" ],
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  },
  "content_scripts":[
      {
         "matches": ["<all_urls>"],
         "js": ["messages.js"],
      }
  ],
  "web_accessible_resources": ["injected.js"]
}

The following shows in the chrome console:

content script:

enter image description here

and background page:

enter image description here

rsanchez
  • 14,467
  • 1
  • 35
  • 46
Cha0s
  • 13
  • 3
  • 1
    @Xan If the `injectCode` function is injecting the content script, I think the problem would actually be http://stackoverflow.com/q/23667086/2336725 – Teepeemm Apr 29 '15 at 14:32
  • @Teepeemm You're right here. Retracting duplicate vote. – Xan Apr 29 '15 at 14:37

2 Answers2

1

You’ve not shown us, but I’m guessing your background script has

function injectCode(tabId) {
    chrome.tabs.executeScript(tabId,{"file":"contentScript.js"});
}

The problem is that this function is asynchronous. The browser doesn’t wait for it to finish, but keeps going with the background script, which sends the message to a content script that isn’t there. Then injectCode finishes, but it’s too late.

Here is a good reference. The short answer is that you need to use the callback option, so that you’ll end up with

chrome.tabs.executeScript(tabId,{"file":"contentScript.js"},function() {
    chrome.tabs.sendMessage(tabId,nodeMessage);
});

But now you'll need to redo your injectCode function to keep track of nodeMessage.

Community
  • 1
  • 1
Teepeemm
  • 4,331
  • 5
  • 35
  • 58
0

If chrome.runtime.onMessage was undefined, you would see an error message in the console telling you that when running the content script.

What's really happening is that you are seeing a limited version of chrome.runtime in the console because you are accessing the webpage context instead of the content script context. See this answer to access the content script context from the console.

Community
  • 1
  • 1
rsanchez
  • 14,467
  • 1
  • 35
  • 46
  • This explains the different consoles, but doesn't address why the message isn't being received. – Teepeemm Apr 30 '15 at 03:26
  • 1
    @Teepeemm that was the title of your question. If you want to ask another thing you should open a new one. – rsanchez Apr 30 '15 at 05:00
  • Turns out an exception in some native code in my background page was preventing the messages, Thanks for the context explanation was exactly what I wanted to know :) – Cha0s May 05 '15 at 11:58