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?