I am having trouble getting my content script request values from my background script.
content_script.js
=================
var elements = undefined
var properties = undefined
var targets = undefined
chrome.extension.sendRequest({greeting: "elements"}, function(response) {
elements = response.input;
});
if (elements == undefined){
var elements = ["a","img"];
}else{
elements = elements.split(',');
}
chrome.extension.sendRequest({greeting: "properties"}, function(response) {
properties = response.input;
});
if (properties == undefined){
var properties = ["alt","id","class"];
}else{
properties = properties.split(',');
}
chrome.extension.sendRequest({greeting: "targets"}, function(response) {
targets = response.input;
});
if (targets == undefined){
var targets = ["onclick","href"];
}else{
targets = targets.split(',');
}...
...More code and references to elements following...
The above code only works when there is a break in the code (ie waiting) before doing any relating to the values set above, I suppose I could put something to do that but i would prefer to use a more efficient solution if possible.
(for reference:)
background.js
=============
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
switch (request.greeting){
case "elements":
var elements = localStorage["elements"];
sendResponse({input: elements});
break;
case "properties":
var properties = localStorage["properties"];
sendResponse({input: properties});
break;
case "targets":
var targets = localStorage["targets"];
sendResponse({input: targets});
break;
}
});
I have been at this for 3 hours (still learning what I’m doing with JS)
Uhhhhhm you cannot do that:
sendRequestis async.And the correct way of checking for undefined is:
If you need all that stuff just make 1 request and do all your stuff inside the callback:
And in background: