Persist data on disk using chrome extension API

841 Views Asked by At

I am trying to save some data which should be available even when restart the browser So this data should persist. I am using Chrome Storage Sync API for this. But when I am restarting my browser, I get empty object on using chrome.storage.get.

Here is my sample code:

SW.methods.saveTaskListStore = function() {
  chrome.storage.sync.set({
    'taskListStore': SW.stores.taskListStore
  }, function() {
    if (SW.callbacks.watchProcessSuccessCallback) {
      SW.callbacks.watchProcessSuccessCallback(SW.messages.INFO_DATA_SAVED);
      SW.callbacks.watchProcessSuccessCallback = null;
    }
  });
};

SW.methods.loadTaskListStore = function() {
  SW.stores.loadTaskListStore = [];

  chrome.storage.sync.get('taskListStore', function(taskFeed) {
    var tasks = taskFeed.tasks; 

    if (tasks && !tasks.length) {
      SW.stores.loadTaskListStore = tasks;
    }
  });
};

I guess I am using the Wrong API.

1

There are 1 best solutions below

1
On BEST ANSWER

If this is not some copy-paste error, you are storing under key taskListStore and trying to get data under key loadTaskListStore.

Besides that, according to the documentation on StorageArea.get(), the result object is an object with items in their key-value mappings. Thus, in your case, you should do:

chrome.storage.sync.get("taskListStore", function(items) {
    if (items.taskListStore) {
        var tasks = items.taskListStore.tasks;
        ...