I set a content_script "run_at": "document_end". It modifies the page, but I want to have a user setting that determines whether the modification should be done or not.
At first, I tried chrome.storage.sync.get
. It was declared at the beginning of the script and the code that begines the modification was declared at the end of the script. (In the middle, there are functions and variables.) But when I tested it, the sync.get
callback was called after the modification code. That is, I get the output like:
Using the value.
Read value from storage.
if the code is like the one at the bottom. I thought it was due to the latency from getting data from a remote Google server, but when I changed sync
to local
, the result was the same. How can I read the setting before doing the actual work?
- Should I begin working when the
storage.X.get
returns, i.e., in its call back? But that would mean unnecessary delay before starting the work. If this is the only way, I will use this. - Isn't there some kind of way to make
storage.X.get
faster, like caching? - If
storage.X.get
does not support caching, how can I cache it myself? The modification is only done once for page loading and the content script gets recreated at each page loading. Is there a way to keep a variable across page loadings in the content script, sort of like a static variable?
.
var modify = true;
//initial read
chrome.storage.local.get("modifySetting",
function(data)
{
console.log("Read value from storage");
if("modifySetting" in data)
{
modifySetting = data["modifySetting"];
}
}
);
... lots of functions and variables ...
console.log("Using the value.");
if(modify)
{
doModification();
}