3

I was experimenting with chrome extension. My goal is to send a message from background script to injected script and let the injected script return back the result.

Code is as below:

background.js

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab){
    chrome.tabs.executeScript(tabId, {'file': "content.js"}, 
        function(){
            chrome.tabs.sendMessage(tabId, {msg: 'test'}, 
                function(response){
                    console.log(response);
                }
            );
            return true;
        }
    );
});

content.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        sendResponse('test message');
        return true;
    }
);

I get the error message

Could not send response: The chrome.runtime.onMessage listener must return true if you want to send a response after the listener returns 

when calling sendResponse inside content.js

I am already returning true from the listener.. confused?
Am I missing something obvious?

vivek.m
  • 3,213
  • 5
  • 33
  • 48
  • that doesnt seem to be the problem. I can remove that line without any effect. The error is in content.js. – vivek.m Jun 19 '13 at 18:11
  • Did you try removing the `return true` in your `onMessage` listener? – BeardFist Jun 20 '13 at 04:11
  • Not sure why you're getting that error, but it might have something to do with the fact that you're generating the listeners on the fly using OnUpdate. You might want to try another approach: Add the content script to the manifest file to load for all URLs when the document runs. Then have the content script send a message to the background page when it's loaded. The background and content script can then communicate normally. – Jude Osborn Jun 20 '13 at 07:45
  • @vivek.m: Did you figure this one out ? – gkalpak Oct 16 '13 at 11:14
  • @ExpertSystem yes, check my answer below. forgot to update SO. – vivek.m Oct 18 '13 at 18:14
  • @vivek.m: Luckily, I got my "sendResponse" working. But thx for the update anyway. – gkalpak Oct 18 '13 at 18:48

1 Answers1

0

I think my issue was fixed by

chrome.tabs.sendMessage(tabId, {action: 'test'}, 

Note, I incorrectly use msg instead of action in above question.

Also, I do not use the third parameter sendResponse to avoid full-duplex communication I simply post another message from content script back to background page.

From content script, send message to background page using:

function SendMessageToBackgroundPage(data)
{
    chrome.runtime.sendMessage({
        action: 'kungfu',
        source: 'panda'
    });
}

Catch it inside background page using:

chrome.runtime.onMessage.addListener(
        function(request, sender, sendResponse) {
            switch (request.action) {
                case 'kungfu': alert(request.source);
            }
        });
gkalpak
  • 47,844
  • 8
  • 105
  • 118
vivek.m
  • 3,213
  • 5
  • 33
  • 48
  • 1
    I believe you should use **chrome.runtime.sendMessage/onMessage** instead of **chrome.extension.sendMessage/onMessage**. Especially when using event-pages (and not background-pages), because **chrome.runtime** functions make sure a suspended background-page is properly loaded before responding/taking any action. – gkalpak Oct 18 '13 at 18:53
  • @ExpertSystem Looks like google has removed reference to extension.sendMessage from [latest docs](http://developer.chrome.com/extensions/extension.html). Updated my answer. – vivek.m Oct 19 '13 at 09:24