Stéphane
Stéphane

Reputation: 131

chrome.runtime.sendMessage raises "Uncaught TypeError: Cannot call method 'sendMessage' of undefined "

I am writing a google chrome extension and trying to send information from a piece of code that is injected into a web page to my content script.

According to http://developer.chrome.com/extensions/messaging#external-webpage, I should use something like :

// The ID of the extension we want to talk to.
var editorExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";

// Make a simple request:
chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
  function(response) {
    if (!response.success)
      handleError(url);
  });

The problem is, when I do :

var script_code = "var msg = {message : 'plop'};\n";
script_code += "chrome.runtime.sendMessage('" + chrome.i18n.getMessage("@@extension_id") + "', msg, function(response) {\n";
script_code += "    console.log('response received');\n";
script_code += "});\n";

An then inject this to the webpage, when it is executed, I get :

Uncaught TypeError: Cannot call method 'sendMessage' of undefined

Can anyone help me through this ?

Thanks

Upvotes: 6

Views: 14456

Answers (3)

Viktor Oleksyshyn
Viktor Oleksyshyn

Reputation: 471

Please check your manifest file and if you are using localhost ensure that matches are correct

Incorrect:

"externally_connectable": {
  "matches": ["https://localhost:PORT_NUMBER/\*"]
}

Correct:

"externally_connectable": {
  "matches": ["\*://localhost/\*"]
}

if its not localhost, please ensure that matches contains mask of at least 2 levels of domains as described in documentation

matches (array of string) - optional

The URL patterns for web pages that are allowed to connect. This does not affect content scripts. If left empty or unspecified, no web pages can connect.

Patterns cannot include wildcard domains nor subdomains of (effective) top level >domains; *://google.com/* and http://*.chromium.org/* are valid, while <all_urls>, >http://*/*, *://*.com/*, and even http://*.appspot.com/* are not.

Upvotes: 4

fulme
fulme

Reputation: 332

javaScript code in Chrome extensions can be divided in the following groups:

  • Extension code - Full access to all permitted chrome.* APIs.
    This includes all extension pages(background page, popup page ,option page, and so on)

  • Content scripts (via the manifest file or chrome.tabs.executeScript) - Partial access to some of the chrome APIs
    Full access to the page's DOM.

  • Injected scripts (via this method in a Content script) - Full access to all properties in the page. No access to any of the chrome. APIs.*
    Execute on the actual page, can't access any of the chrome.* APIs.**.

In your case, code is execute on the actual page, can't call chrome.runtime.* APIs.
Maybe, you can try window.postMessage().

Upvotes: 10

St&#233;phane
St&#233;phane

Reputation: 131

Well, in fact, I included the wrong version of the manifest.json file.

My bad, this is where you specify that you want to expose the messaging API to certain sites.

Problem solved.

Upvotes: -4

Related Questions