0

UPDATE

From what I can tell, it is impossible to send a message from the background script to the content script using the "sendMessage" function. However there is a horrible workaround,

In your content script's window.onload, send a message to the background script:

chrome.runtime.sendMessage( { action: "messaging", window: "app" }, this.listenForFutureMessages );

Also in the content script, have the following function:

listenForFutureMessages: function(someAction)
{
    //Take some action based on the message

    //If we want the background script to be able to contact
    //us again, we need to give them another callback. This
    //is because Chrome only allows one use per callback
    chrome.runtime.sendMessage( { action: "messaging", window: "app" }, this.listenForFutureMessages );
},

In the background script, have a listener that does something like this:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse)
    {
        if ( request.action === "messaging" )
        {
            //Save the callback for later
            this.listeners[ request.window ] = sendResponse;

            //Tell chrome we will be using the callback later
            return true;
        }
    }
);

When your background script wants to send the content script a message, simply call it like this:

this.listeners[ "app" ]( { someProperty: "some value" } );

This is a stupid way to do this, but it makes this actually possible. Hope this helps anyone else who needs this functionality.

ORIGINAL I'm unable to send a message from my background script to a content script. When I try to find the tab id, it tells me I don't have permissions even though my app has that permission. And when I receive a message from the content script, and print out the sender object, it shows tab.id = -1. The API to send a message to a content script requires a tab id!

chrome.tabs.sendMessage(integer tabId, any message, function responseCallback)

The error:

chrome.tabs is not available: You do not have permission to access this API. Ensure that the required permission or manifest property is included in your manifest.json.

Error in event handler for 'undefined': Cannot call method 'sendMessage' of undefined TypeError: Cannot call method 'sendMessage' of undefined at chrome-extension://panoaieakcofaegcjfbmhndaekfgpijh/scripts/background.js:109:16 at Event.dispatchToListener (event_bindings:356:21) at Event.dispatch_ (event_bindings:342:27) at Event.dispatch (event_bindings:362:17) at miscellaneous_bindings:167:33 at Event.dispatchToListener (event_bindings:356:21) at Event.dispatch_ (event_bindings:342:27) at Event.dispatch (event_bindings:362:17) at Object.chromeHidden.Port.dispatchOnMessage (miscellaneous_bindings:253:22)

So how do I contact my content script? (I have multiple windows and need to be able to contact them individually)

My manifest:

{
    "manifest_version": 2,
    "name": "App",
    "description": "App",
    "version": "0.75",
    "minimum_chrome_version": "27",
    "offline_enabled": true,
    "icons": 
    {
        "16": "images/icon16.png",
        "48": "images/icon48.png",
        "128": "images/icon128.png"
    },
    "app": 
    {
        "background": 
        {
            "scripts": 
            [
                "scripts/background.js"
            ]
        }
    },
    "permissions": 
    [
        "unlimitedStorage",
        "fullscreen",
                {
            "fileSystem": 
            [
                "write"
            ]
        },
        "background",
        "<all_urls>",
        "tabs"
    ],
    "update_url": "http://192.168.1.121/app.xml"
}
2
  • Don't put your answer inside the question. If you have an answer, post it as answer. Commented Jul 1, 2013 at 0:19
  • @kiamlaluno I don't consider it an answer. It's a workaround. Commented Dec 11, 2013 at 20:05

1 Answer 1

1

There's not such a thing called "Content scripts" in a Chrome app. Your manifest file looks like a mixture of a Chrome extension. Open chrome://extensions/, enable developer mode, and you would see a warning that the "background" and "tabs" permissions are invalid for a Chrome app.

If you're implementing a Chrome app, just use chrome.runtime.sendMessage and chrome.runtime.onMessage. These messages can be send from and to your event page and the main page. For example:

// event page (aka background page)
chrome.app.runtime.onLaunched.addListener(function() { 
    chrome.app.window.create('main.html');
});

    // Later, when you want to notify the app window
    chrome.runtime.sendMessage(" ... any message ... ");
<!-- main.html -->
<script src="main.js"></script>
// main.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    // Do something with the message
});
Sign up to request clarification or add additional context in comments.

4 Comments

This does not work for me (it's exactly what I tried to do). The main.js never receives the message. I may have used the wrong terminology but my"main page" never receives that message.
@DonRhummy Then show your real manifest and code instead of something else that doesn't seem to represent your code.
That is my manifest. You'll notice it does not declare any content scripts. Even removign the background and tabs permissions does not make this work.
@DonRhummy Please provide your source code in your question. Otherwise, I can only guess what's wrong with your code (my attempt to reproduce your environment failed, so it's up to you to provide a self-contained example which demonstrates your issue).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.