1

I am trying to make an extension where the user enters some data(website URLs) through a text input located in the popup menu. I want this data to be stored permanently throughout the life of the extension and not be erased when the user closes the browser. I want the data that the user entered into the pop-up up menu to be displayed each time the user opens up the menu. Any advice is appreciated!

  • I mean if you use chrome.storage, what seems to happen when you are retrieving the data is that it gives you an object with "newValue" and "oldValue" in it. The oldValue seems to have data from a previous session, but as soon as you enter new data, that "oldValue is replaced with a value you had just previously entered. There seems to be now way to get it back. I do not understand – Tony Kappen Jan 20 '17 at 00:07
  • @Makyen, Thank you! – Tony Kappen Jan 20 '17 at 00:26
  • You should only be getting `newValue` and `oldValue` properties from the [`chrome.storage.onChanged`](https://developer.chrome.com/extensions/storage#event-onChanged) event. Are you getting those properties somewhere else? I'm not really sure what your comment question is asking. – Makyen Jan 20 '17 at 00:37
  • Hi @Makyen, I had been using the onchanged event to trigger the chrome.storage.set. I changed that part now – Tony Kappen Jan 20 '17 at 01:40

1 Answers1

1

The code is quite simple.

To store it:

chrome.storage.sync.set(
    {
        varNameOne : var1,
        varNameTwo : var2
    },
    function () {
        // callback code
    }
);

And to get the data back:

chrome.storage.sync.get(
    {
        varNameOne : "",    // if there isn't any data,
        varNameTwo : ""     // what you set here will be returned
    },
    function (items) {
        varNameOne = items.varNameOne;
        varNameTwo = items.varNameTwo;
        // callback code
    }
);

But remember it is asynchronous, so any code you write after the chrome API call will happen before the call gets made

Jason
  • 779
  • 1
  • 9
  • 30
  • This would be better if you used `storage.local` in your example. Even better would be to explain the difference between the two and discuss the use of `localStorage` in the background page. – Makyen Jan 20 '17 at 02:26