I've sent a request from my content script and received a response from my background page successfully. Now I want to set a variable in my content script - based on the response. That's where I'm having trouble. Should be simple enough.... I guess I must be getting the syntax wrong. Here's what's in my content script:
chrome.extension.sendRequest({greeting: "hello"}, function(response) {
console.log(response.farewell);
if (response.farewell == "goodbye") {
wasPolite = "yes";
}
else if (response.farewell == "goaway") {
wasPolite = "no";
}
else {
wasPolite = "maybe";
}
});
alert(wasPolite);
Console shows goaway
as the response, but wasPoite
remains undefined.
If you define:
var wasPolite = '';
Before the:
chrome.extension....
and it's still not alerting the correct value then I'd ask is the chrome.extension.sendRequest() function asynchronous? If so you may be alerting wasPolite before the request has had time to callback. @Ruup is probably on track suggesting that wasPolite will only have it's value after the sendRequest has actually finished thereby triggering the callback