The problem is that your callback is called after the main function returns. (Extension APIs are called asynchronous for some reason!) returnVal is undefined because it has not been assigned yet. Try changing your function to accept the callback argument:
function getCookie (cookieName, callback){ chrome.cookies.get({ 'url':'https://addictedtogether.com/', 'name':cookieName }, function(data){ callback(data); }); }
If you don't like passing callbacks, you can also change your function to return a delayed one. If you have to handle many asynchronous function calls, deferrals make your life easier. Here is an example using jQuery.Deferred:
function getCookie (cookieName){ var defer = new jQuery.Deferred(); chrome.cookies.get({ 'url':'https://addictedtogether.com/', 'name':cookieName }, function(data){ defer.resolve(data); }); return defer.promise(); }
source share