I have an interesting challenge for all of you JavaScript, jQuery and Google Chrome extension gurus!
I am trying to get a value from the chrome.storage API and assign it to a variable. Here's the scenario, when I am in debug mode and step through my code everything works fine, but when I am not in debug mode I don't get the correct return value. What I find really interesting is that if I put in an alert message between the assignment of the variable and the if statement that it also works. Also, if I try to set a timeout or kill time with a while loop the results are the same.
This may be happening because of asynchronicity, as the docs on the Google site state, even though I am using a return value as I am directed to do. I have also tried LocalStorage and SendMessage from my inject.js to my background.js and I get the exact same results.
There are other methods that I've used to try and store my variable, such as, writing a text file to the user's local computer and accessing the clipboard copy and paste functions. These gave me security errors as injected code can't access these functionalities for the obvious reasons.
So, if anyone can tell me how to store a string variable and recall it later on using chrome.storage or using any other method, please let me know!
Thanks in advance!
Here's my code...
//GET A PREVIOUSLY STORED IFRAME ID
var iFrameID = "";
chrome.storage.local.get('iFrame', function (result) {
iFrameID = result.iFrame;
//alert(result.iFrame); <-- AN ALERT HERE WORKS
});
//alert(iFrameID); <-- AN ALERT HERE ALSO WORKS
//CHECK TO SEE IF THE PREVIOUS IFRAME ID EXISTS
if (iFrameID == undefined) {
iFrameID = "";
}
//UPDATE THE DOM WITH THE PREVIOUS IFRAME ID
$(".div_iFrameID").val(iFrameID);