Hi again,
I was trying to see where I went wrong with my messaging part, so, I created a simple test extension:
manifest.js
========
{
"name": "Content Script",
... // all the necessary things here
"background": {
"page": "background.html"
},
"content_scripts": [
{
"js": ["page_contentscript.js"]
}
],
"permissions": [
"background","tabs","<all_urls>"
]
}
background.html
============
<!doctype html>
<html>
<body>
<script src="extension_contentscript.js" type="text/javascript"></script>
</body>
</html>
page_contentscript.js
================
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
extension_contentscript.js
===================
chrome.extension.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"});
});
I loaded the extension and I checked the console:
Uncaught TypeError: Cannot call method 'addListener' of undefined
exactly at the line: chrome.extension.onMessage.addListener
More than that. I started index.html on 127.0.0.1 and I got:
Uncaught TypeError: Object #<Object> has no method 'sendMessage'
at the line: chrome.extension.sendMessage
As you can see, it's nothing else but copy-paste from
http://code.google.com/chrome/extensions/messaging.html (chapter "Simple one-time requests") and it seems the "extension" object from within "chrome" object is not recognized at all. Either I missed something in the documentation, or the example from there doesn't work. In either case I am pretty much frustrated, so, any help in understanding how that works is much appreciated.
I am using Chromium 18.0.1025.168 (Developer Build 134367 Linux) Ubuntu 12.04.
Regards,
CGS