0

I have following code in my Chrome extension

devtoolsPanel.js

chrome.runtime.sendMessage({
        name: 'executeScript',
        tabId: chrome.devtools.inspectedWindow.tabId
    }, function(response) { 
       if(response != undefined){
           console.log(response.message);         
       }
   });

background.js

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    try {
        if (message.name == "executeScript") { 
            chrome.tabs.executeScript(message.tabId, {
                    code: 'document.body.style.backgroundColor="red"'
                }, function() {                
                sendResponse({"message":"script executed"});    
            });            
        }
    } catch (e) {
        sendResponse({
            "exceptionD": "" + e
        });
    }
});

I would expect that sendResponse() gets called after executing content script but it gets called immediately after chrome.tabs.executeScript() with undefined.

This happens with any runtime API's that I use in content script. I tries with Localstore API also, same thing happens

for example in my contentscript.js I have the following code

function readOptions(sendResponse) {  
    var options = {};
    chrome.storage.local.get(null, function(items) {
        sendResponse(items);
    });
}

Even in this case sendResponse() gets called immediately after chrome.storage.local.get() with undefined.

Can some one tell me whats happening here.

surya
  • 123
  • 10
  • You specifically test that `response!=undefined`. I'm not sure what you mean is getting called "with undefined". What console log are you seeing? What console log are you expecting to see? – Teepeemm Jun 16 '16 at 22:27
  • http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-response-not-sent Answered my questions. I just need to add return true; in chrome.runtime.onMessage.addListener() – surya Jun 17 '16 at 02:29

0 Answers0