1

Google's documentation on Chrome extension development has information on passing messages between a background script and a content script. I'm trying the example code:

background.js

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
        console.log(response.farewell);
    });
});

content_script.js

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"});
        //return true;
    });

They don't include a manifest file, so I created one (and three icons):

{
    "background": {
        "persistent": false,
        "scripts": ["background.js", "content_script.js"]
    },
    "content_scripts": [ {
        "js": ["content_script.js"],
        "matches": ["<all_urls>"]
    } ],
    "description": "Test passing messages between a background script and a content script using Google's example code.",
    "icons": {
        "16": "img/icon_16x16.png",
        "48": "img/icon_48x48.png",
        "128": "img/icon_128x128.png"
    },
    "manifest_version": 2,
    "name": "Test Google Chrome extension messaging",
    "permissions": ["notifications"],
    "version": "2017.8.25"
}

But when I load this extension, the Console reports:

Error in event handler for (unknown): TypeError: Cannot read property 'farewell' of undefined at chrome-extension://ibocemgcnbijkacfkpokffkfkinmenlc/background.js:3:29

I searched online for other cases of the response being undefined, and a few sources recommended adding return true;. I tried that, but it made no difference (in the code above, it's commented out).

Can anyone see how I could get this working? Thanks!

avidcoder
  • 123
  • 1
  • 9
  • 1
    I can't see how your content_script is going to end up being injected into any tab - unless I'm missing something, shouldn't you have a `"content_scripts"` in your manifest? – Jaromanda X Aug 26 '17 at 01:32
  • or perhaps a `chrome.tabs.executeScript` in the background.js - the content_script has to get "in" the content somehow – Jaromanda X Aug 26 '17 at 01:39
  • I do have a `content_scripts.js` in the manifest. I'm new to Chrome extensions, so I could be wrong, but I assume if additional code were needed, Google would have included it. – avidcoder Aug 26 '17 at 02:16
  • 2
    no, you have a file called `content_script.js` in the `background` property - `content_script.js` isn't a *special name* - perhaps you need to read more about `manifest.json` – Jaromanda X Aug 26 '17 at 02:17
  • Yes, I wasn't thinking `content_script.js` is a special name and thus added to the background page automatically. That's why I included it in the `background` property. The [docs](https://developer.chrome.com/extensions/background_pages) state that "A background page will be generated by the extension system that includes each of the files listed in the scripts property." So by listing `content_script.js`, isn't that sufficient to make it part of the background page? – avidcoder Aug 26 '17 at 02:40
  • oh, so you want content_script in background - I thought you wanted it injected into a tab – Jaromanda X Aug 26 '17 at 03:03
  • 2
    It doesn't make any sense to include a file named content_script in the background page. It's not an actual content script, it runs in the same background page, not in a web page, and it won't receive messages sent from the same background page context. This is why you get an undefined value. Continue reading the documentation about the content scripts. – wOxxOm Aug 26 '17 at 04:03
  • Thank you for the valuable feedback. (I have been trying to find complete documentation on content scripts, but the Google docs seem quite sparse.) So the [example code](https://developer.chrome.com/extensions/messaging) is incomplete? In other words, it is missing some additional code for adding the content script to the web page? – avidcoder Aug 26 '17 at 14:58
  • The code in [Message Passing](https://developer.chrome.com/extensions/messaging) assumes that you have read and apply the information in the [Chrome extension overview](https://developer.chrome.com/extensions/overview) (perhaps along with the pages linked from the overview) and [Content Scripts](https://developer.chrome.com/extensions/content_scripts). In particular, [Content Scripts](https://developer.chrome.com/extensions/content_scripts) provides information on how to inject content scripts into the page. – Makyen Aug 26 '17 at 20:31
  • My initial manifest file was missing a "content_scripts" property, so I added it now, to inject `content_scripts.js` into the tab. Unfortunately, the code still gets the same results of the response being undefined. – avidcoder Aug 28 '17 at 01:37

0 Answers0