0

I have an app and an extension for google chrome and I want the app to request the value of a cookie from the extension and get the value as response. This is my current code and it does not work:

app.js:

Helper: {
    ...
    requestSessionId: function(sub){//pass subdomain of the cookie
        console.log('request session id for ' + sub + '.example.com');
        chrome.runtime.sendMessage('myextensionkeyorwhateverthisis',
            {getSessionId: {subdomain: sub}},
            function(response) {
                console.log('response: ', response); //nothing to see here
            }
        );

    },
    ...
}

extension.js:

chrome.runtime.onMessageExternal.addListener(
    function(request, sender, sendResponse) {
        if(sender.id == 'theappkeyorhowthisisnamed'){
            if(request.getSessionId){
                chrome.cookies.getAll({domain: request.getSessionId.subdomain + '.example.com', name: 'sid'}, function(res){
                    console.log(res[0].value);// I see this in my developer console,
                    sendResponse(res);        // but this is either not send or not received, for I cannot see any output in the app, as noted above
                    return;
                });
            }
        }
    }
);

I am sure that the connection between app and extension works, because I added a Listener in the extension that replied to all Messages with a dummy value and it worked. So I assume that the problem lies somewhere in the callback of the cookie request, however I don't get any errors, so I don't know what it is. Maybe the sendResponse does not work in the scope of the callback?

What do I have to do for this to work? I would also appreciate every answer that shows me how to do this in another way.

user1950929
  • 874
  • 1
  • 9
  • 17
  • Put `return true;` at the end of your callback. I'm sure that this question has been asked before, let me search for a duplicate. – Rob W Dec 30 '13 at 16:33
  • 1
    A couple more :) http://stackoverflow.com/a/20077854 http://stackoverflow.com/a/20695181 – rsanchez Dec 30 '13 at 20:23

0 Answers0