0

Case 1

I am using this snippet to send a request to my background page, and receive a message

chrome.runtime.sendMessage({details: "Command1"}, function(response) {
              console.log(response.farewell);
});

This works perfectly when it is called stand alone.

Here is my background page code which responds to the request :-

if(typeof(request.details) != "undefined"){
        console.log("Detail Array was asked for !");
        sendResponse({farewell: "Some Data here"});
        console.log("Respo 1 sent");
    } 

As expected, on calling details : command1 from content script page, I get the expected output "Some Data here", back in my content scripts

Case 2

I am trying to generalize this for a number of requests, so, I did a change in my content scripts as shown :-

function sendMessage(toSendKey, toSendVal, funcName){
chrome.runtime.sendMessage({toSendKey: toSendVal}, function(response) {
           console.log("Response received");
           respoObt = response.farewell;
           console.log(respoObt);
           funcName(respoObt);
         });

}

function doNothing(data){
    console.log(data);
}

sendMessage("details", "Command1", doNothing);

Now on calling the function with appropriate parameters, I can see the two console logs ("Detail Array was asked for !", "Respo 1 sent") in my background page, confirming that the request has reached it, but, the response sent back is not able to reach to content script page.

What could be the possible culprit ? Am I missing something ?

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Prashant Singh
  • 3,725
  • 12
  • 62
  • 106

0 Answers0