Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.runtime.sendMessage in content script doesn't send message

I have the following files (gist for easy access):

manifest.json

{
  "name": "testmessage",
  "version": "0.1",
  "manifest_version": 2,
  "externally_connectable": {
    "matches": ["*://www.google.com/*"]
  },
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  },
  "content_scripts": [
    {
      "matches": ["*://www.google.com/*"],
      "js": ["content.js"]
    }
  ]
}

content.js

chrome.runtime.sendMessage(
    "eldkfaboijfanbdjdkohlfpoffdiehnb", // PUT YOUR EXTENSION ID HERE
    "foo",
    function (response) {
        console.log(response);
    }
);
console.log("this is content.js reporting for duty");

background.js

chrome.runtime.onMessageExternal.addListener(
    function(request, sender, sendResponse) {
        console.log("background.js got a message")
        console.log(request);
        console.log(sender);
        sendResponse("bar");
    }
);
console.log("this is background.js reporting for duty");

I can see both "... reporting for duty" messages in the respective consoles. But background.js doesn't receive a message when http://www.google.com loads. Line 5 in content.js prints undefined in the console for google.com.

When I run chrome.runtime.sendMessage("eldkfaboijfanbdjdkohlfpoffdiehnb", "foo"); in the google.com console it shows up in the background.js console.

What am I doing wrong?

like image 554
dAnjou Avatar asked May 11 '14 19:05

dAnjou


People also ask

What is chrome runtime sendMessage?

runtime. onMessage API functions. The chrome.runtime.sendMessage function is used to send one time messages from one part of the extension to another part. The function receives a message object which can be any JSON serializable object and an optional callback to handle the response from the other part.

What are content scripts?

A content script is a part of your extension that runs in the context of a particular web page (as opposed to background scripts which are part of the extension, or scripts which are part of the website itself, such as those loaded using the <script> element).


1 Answers

What you're doing wrong is over-complicating it. Twice.

First, you don't need to declare to be externally connectable, since you're sending a message from a content script and not the webpage itself.

Second, it's not an external message either. External messages are for connecting different extensions, not messages within one extension.

Your code should look like this:

content.js

chrome.runtime.sendMessage(
    "foo",
    function (response) {
        console.log(response);
    }
);

background.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.log("background.js got a message")
        console.log(request);
        console.log(sender);
        sendResponse("bar");
    }
);
like image 64
Xan Avatar answered Oct 07 '22 18:10

Xan