I have used message passing in my chrome extension to move data from the background.js to the content.js file.
Within the function below when I output the variable to the console within the function I get the data I expect. When I try the same outside of the function I get null (as defined in the global variable).
Does anyone have an idea how I can pull the data out of the function so I can post it with my AJAX call.
Code
var capturedData = "url=" + window.location.href;
var localHash = "localhash=" + CryptoJS.MD5(capturedData);
var email = null;
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
email = response;
console.log(JSON.stringify(email));
return true;
});
console.log(JSON.stringify(email));
$.ajax({
type: 'post',
url: 'url',
data: {"url":capturedData, "localhash":localHash},
dataType: 'json',
success : function (data) {
if(data.url)
{
console.log(data.url);
}
}
});
Output
null content.js:10 {"email":"[email protected]"} content.js:7
Thanks!