content script question

589 views
Skip to first unread message

CGS

unread,
Jul 27, 2012, 10:30:03 AM7/27/12
to Chromium-extensions
Hi everyone,

I am trying to do some messages parsing in between the extension popup and a webpage designed to interact with the popup. Reading the documentation, I found that the recommended way is via content script (http://code.google.com/chrome/extensions/content_scripts.html), but I got this from the popup:

Uncaught TypeError: Cannot call method 'addListener' of undefined

The manifest permissions:

    "permissions": [
        "background","tabs","<all_urls>"
    ],

The popup is loading a javascript file in which a function calls (one message off at the time):

   chrome.extension.onMessage.addListener(
        function(request, sender, sendResponse) {
              // do something with the request and answer back via sendResponse
    });

Am I missing anything?

CGS

CGS

unread,
Jul 27, 2012, 6:21:24 PM7/27/12
to Chromium-extensions
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": [
        {
            "matches": ["http://127.0.0.1/*"],
            "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

CGS

unread,
Jul 27, 2012, 6:54:27 PM7/27/12
to Chromium-extensions
Nevermind, I found the problem: Chromium has no extension object. Everything works fine in Google Chrome for Linux. That is pretty much a mess considering they are both shipped from Google.

CGS
Reply all
Reply to author
Forward
0 new messages