chrome.runtime.sendMessage not working as expected

Jiby Jose picture Jiby Jose · Mar 30, 2013 · Viewed 25.2k times · Source

I am writing a Chrome plugin with a content script and a background script, and I am trying to make the two communicate.

In my content script, I am doing

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
      console.log(response.farewell);
});

and in my background script, I am doing

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.log(sender.tab ?
          "from a content script:" + sender.tab.url :
          "from the extension");
        if (request.greeting == "hello")
            sendResponse({farewell: "goodbye"});
    }
);

My manifest looks like this:

{
    "manifest_version": 2,
    "name": "Tesing Phase",
    "version": "1.0",
    "background": {
        "persistent": false,
        "scripts": ["bgscript.js"]
    },
    "content_scripts": [{
        "js": ["contentscript.js"],
        "all_frames": true,
        "run_at" : "document_start",
        "matches": ["*://*/*"]
    }],
    "web_accessible_resources": ["script.js"]
}

When I run my plugin, I get the following error:

Uncaught TypeError: Object #<Object> has no method 'sendMessage' 

I tried logging chrome.runtime, and there was no method sendMessage. I am using version 25.0 of Chromium on Ubuntu. I tried using sendRequest as well, but it said it's depreciated and sendMessage should be used.

Can anyone point me out what I am missing here? Are there any permissions needed for this to work?

Answer

Rob W picture Rob W · Mar 30, 2013

chrome.runtime.sendMessage / onMessage (and other related events/methods such as connect) were introduced in Chrome 26.

If you want to write an extension which is compatible with Chrome 20 - 25, use chrome.extension.sendMessage.

A way to achieve optimal compatibility is to define the chrome.runtime methods yourself. For example, run the following code before the rest of your code (background/content script):

if (!chrome.runtime) {
    // Chrome 20-21
    chrome.runtime = chrome.extension;
} else if(!chrome.runtime.onMessage) {
    // Chrome 22-25
    chrome.runtime.onMessage = chrome.extension.onMessage;
    chrome.runtime.sendMessage = chrome.extension.sendMessage;
    chrome.runtime.onConnect = chrome.extension.onConnect;
    chrome.runtime.connect = chrome.extension.connect;
}

Then you can just use the latest API format:

// Bind event:
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    // Do something
});

// Send message:
chrome.runtime.sendMessage({greeting: 'hello'});

If you feel uncomfortable with modifying methods on the chrome.runtime object, you can use the following approach instead:

var runtimeOrExtension = chrome.runtime && chrome.runtime.sendMessage ?
                         'runtime' : 'extension';

// Bind event:
chrome[runtimeOrExtension].onMessage.addListener(
  function(message, sender, sendResponse) {
    // Do something
});

// Send message:
chrome[runtimeOrExtension].sendMessage({greeting: 'hello'});