I am currently converting one of my userscripts to a chrome extension. The only thing I am still stuck with is successfully storing the localStorage values in variables in my content script. I will give one example here, the other ones are similar but with other names.
var REMOVE_WARNINGS;
chrome.extension.sendRequest({method: "REMOVE_WARNINGS"}, function(response) {
console.log(response.status);
REMOVE_WARNINGS = response.status == "true";
});
As you can see, I query the localStorage of my extension for the value that was assigned to “REMOVE_WARNINGS”. I do that because further on in the content script, this variable gets checked to see whether it has to perform certain actions or not. The original value gets assigned in the localStorage through the options page by a HTML option, either true or false, which happens without a problem. The value “true” (or “false”) gets successfully returned to the content script’s method and printed in the console, because I have checked it. What DOESN’T work is assigning the value to the global variable REMOVE_WARNINGS in my content script.
I have tried a few options, including REMOVE_WARNINGS = response.status;, REMOVE_WARNINGS = response.status == "true"; and I have also tried to remove the console output line thinking that it might print destructively. None of these options seems to successfully store the boolean value “true” or “false” in the global variable, because the script does not perform the actions it should do.
Does anyone see what I’m doing wrong here? I’m completely lost at this point, especially since clearly reading the value is no problem. It’s storing it in a global variable of the content script, that is the problem.
Cheers
Kenneth
Please correct me if I’m wrong but I think you’re trying to do something like:
Try to modify your code like this: