0

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);
Jay
  • 548
  • 5
  • 11
  • No matter how hard you try to battle asynchronicity, there's only one thing to do - understand it, and embrace it. The canonical question I linked to will help you with that; this is not specific to Chrome APIs – Xan May 11 '16 at 13:58
  • Thanks for your reply, I read the extremely long post you referenced. Very informative. Although similar to my problem it is not an exact duplicate as the post referenced problems of a console.log message returning undefined outside of an asynchronous call, in my case if there was an alert message the variable returned properly, if I removed the alert then I got undefined. Also, in the case of a google chrome extension none of the six replies from the post worked correctly when I implemented them, I got the same results every time. Thanks again for pointing me in the right direction. Cheers! – Jay May 12 '16 at 17:39
  • Alert pauses execution, which may mess up timing. If you think you implemented something according to that post and it doesn't work, please make a new question (mentioning that post, so people don't just point you there) and we'll help! – Xan May 12 '16 at 17:40
  • Hello everyone, I was finally able to make this work, the solution was two fold, first, by using a Promise (although it in itself it did not work), and second, to use the Promise in a different event listener linked to the mousedown event and leaving the rest of the code in the mouseup event! – Jay May 12 '16 at 17:40

0 Answers0