0

I've been trying to use chrome.storage.sync.get for the options part of my program but I've hit a snag.

document.addEventListener('DOMContentLoaded', function() {
document.getElementById("save").addEventListener("click", save_options);
});
function save_options() {
   var priceNotif = document.getElementById('pricealert').value;
   chrome.storage.sync.set({
   priceAlert: priceNotif
}, function() {
// Update status to let user know options were saved.
   console.log("price set: " + priceNotif);
   setTimeout(function() {
  status.textContent = '';
}, 750);
});
}
var test = chrome.storage.sync.get('priceAlert', function(data) {
test = data.priceAlert;
});
console.log("test price get: " + test);

The test variable is returning undefined, and how do I fix that?

  • Callbacks in extensions API are invoked asynchronously, there's no immediate return value, the result is available only inside the callback, see [How do I return the response from an asynchronous call?](//stackoverflow.com/q/14220321) – wOxxOm Aug 09 '18 at 12:30
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Josh Lee Aug 09 '18 at 12:40

1 Answers1

-2

if you are creating chrome extension then you can use Please confirm that you need to add needfull permission in manifest.JSON file. please refer below document.

https://developer.chrome.com/extensions/storage

Pankaj Rupapara
  • 752
  • 4
  • 9