Exception
Exception

Reputation: 8389

Sending request on load of a popup.html to content script receives nothing in this case

My Requirement is whenever I click on the Extension Icon that should send a Request to content script and that should send reply with required properties. I am able to send the request. And when I check console Content script is recieving the request and processing it.But At popup side I could not receive anything.
Here is the request handler in Content script

chrome.extension.onRequest.addListener(function ListeningMethod(request, sender, callback)
{
    switch(request.action)
    {
        case "QuestionProperties":
            sendResponse({attributes: {"h":"s","r":"t"} });
        break;
    }
});

And on popup.html I am sending the request like this

$(document).ready(function(){
        chrome.tabs.getSelected(null, function(tab) {
            chrome.tabs.sendRequest(tab.id, {action: "QuestionProperties"}, function(response){
                alert('received something'); // Even this is not alerting
                                    var data = JSON.parse(response.attributes);
                alert(JSON.stringify(data)); // Here also I could not recieve anything.  At Contentscript side I have checked the response that is being sent. I am able to see the data. But at Popup side I am unable to recieve it. Please help me on this. 
            });
        });
 });

Upvotes: 0

Views: 390

Answers (1)

abraham
abraham

Reputation: 47893

Your content_script isn't calling the correct method to send the response. Your listener function is naming it callback but then trying to use sendRequest. You should also either remove the function name or define it outside of the addListener for clarity.

chrome.extension.onRequest.addListener(function(request, sender, callback)
{
    switch(request.action)
    {
        case "QuestionProperties":
            callback({attributes: {"h":"s","r":"t"} });
        break;
    }
});

Upvotes: 1

Related Questions