Google chrome extension 使用chrome扩展API在磁盘上持久化数据

Google chrome extension 使用chrome扩展API在磁盘上持久化数据,google-chrome-extension,local-storage,chrome-sync,Google Chrome Extension,Local Storage,Chrome Sync,我正在尝试保存一些数据,即使重新启动浏览器,这些数据也应该可用,以便这些数据应该保持不变。我正在为此使用Chrome存储同步API。但是,当我重新启动浏览器时,我使用chrome.storage.get获得空对象 以下是我的示例代码: SW.methods.saveTaskListStore = function() { chrome.storage.sync.set({ 'taskListStore': SW.stores.taskListStore }, function()

我正在尝试保存一些数据,即使重新启动浏览器,这些数据也应该可用,以便这些数据应该保持不变。我正在为此使用
Chrome存储同步
API。但是,当我重新启动浏览器时,我使用
chrome.storage.get
获得空对象

以下是我的示例代码:

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;
    }
  });
};

我猜我使用了错误的API。

如果这不是复制粘贴错误,那么您将存储在key
taskListStore
下,并尝试在key
loadTaskListStore
下获取数据

此外,根据上的文档,result对象是一个在键值映射中包含项的对象。因此,在您的情况下,您应该:

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

是的loadTaskListStore是一个输入错误..编辑了这个问题..我想你是对的,我用错了API。让我再试试你的建议