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