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 , 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
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./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
Share Improve this question asked Feb 20, 2014 at 23:28 StéphaneStéphane 1311 gold badge2 silver badges5 bronze badges 2- Content scripts execute separately from javascript in a webpage, when you inject a script into a page it cannot talk directly to your content script. There are usually work arounds so post more details as to what you are trying to acplish. – 1337holiday Commented Feb 21, 2014 at 6:38
- Yes @1337holiday , I know, that is why I am trying to use this messaging API to send my data from the web page (through this injected script), to the event/background page, and then resend it from there to my content script, like it is described in the documentation. If there is a simpler way that is also safe, why not :-) – Stéphane Commented Feb 21, 2014 at 8:35
3 Answers
Reset to default 10javaScript 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().
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./* and http://*.chromium/* are valid, while <all_urls>, >http://*/*, *://*./*, and even http://*.appspot./* are not.
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.