简体   繁体   English

Chrome扩展程序弹出窗口未使用chrome.storage.sync保留数据集

[英]Chrome extension popup not persisting data set with chrome.storage.sync

I'm trying to build a Chrome extension that consists mainly of a popup app. 我正在尝试构建一个主要由弹出式应用组成的Chrome扩展程序。 The problem is that whenever I click out of the popup (close it) the data I set disappears. 问题是每当我点击弹出窗口(关闭它)时,我设置的数据就会消失。 Using the Storage Explorer extension as well as my own debugging console logs I can see for sure that it's getting set to chrome.storage.sync, but every time I reopen it all that data is gone. 使用Storage Explorer扩展以及我自己的调试控制台日志,我可以确定它已设置为chrome.storage.sync,但每次重新打开它时,所有数据都消失了。

Am I fundamentally misunderstanding how the API is meant to be used? 我是否从根本上误解了如何使用API​​? I'm using React and building to a popup.html, I don't have a content.js or background.js. 我正在使用React并构建到popup.html,我没有content.js或background.js。 Do I instead need to be sending around messages for the data to persist? 我是否需要发送消息才能保留数据?

Here is the relevant code in my React App.jsx. 这是我的React App.jsx中的相关代码。 I have a sync function that I'm using to sync allData (which is just an array of objects) and a data getter in componentWillMount: 我有一个同步功能,我用来同步allData (只是一个对象数组)和componentWillMount中的数据获取器:

const syncChanges = (allData) => {
  chrome.storage.sync.set({ allData }, () => {
    chrome.storage.sync.get('allData', data => console.log(data));
  });
};

componentWillMount() {
  // On app load, check if there's existing data. If not, set it.
  chrome.storage.sync.get('allData', (allData) => {
    console.log(allData);
    if (allData.length) {
      this.setState({ allData });
    } else chrome.storage.sync.set({ allData: [] });
  });
}

Something else noteworthy, in the details of the extension it doesn't show the storage permission even though I set it in my manifest.json, and it's not giving me errors on using the API. 其他值得注意的是,在扩展的细节中它没有显示存储权限,即使我在manifest.json中设置它,并且它没有给我使用API​​的错误。 Below is my manifest.json in full, but it seems fine to me. 下面是我的manifest.json,但对我来说似乎很好。

{
  "name": "Leetcode Reminders",
  "description": "A Chrome extension to remind users about Leetcode problems they should revisit.",
  "manifest_version": 4,
  "browser_action": {
    "default_popup": "index.html",
    "default_title": "Leetcode Reminders"
  },
  "version": "1.0",
  "permissions": [
    "storage",
    "declarativeContent",
    "tabs"
  ],
  "icons": {
    "64": "favicon.png"
  }
}

The chrome.storage.sync.get callback is passed an object with the data as key-value properties not your data directly. chrome.storage.sync.get回调传递一个对象,其中数据作为键值属性,而不是直接传递给您的数据。 Thus your allData variable is not your saved array, but an object that contains it. 因此,您的allData变量不是您保存的数组,而是包含它的对象。 As such it has no length property (unless you saved data named length ). 因此它没有length属性(除非您保存了名为length数据)。

So on each opening you are resetting your data back to an empty array as if(allData.length) will test as false because length doesn't exist. 因此,在每个开口处,您将数据重置为空数组,就if(allData.length)将测试为false,因为length不存在。

Check the appropriate property name, in your case .allData , to see if you have already set the data 检查相应的属性名称(在您的情况下为.allData ,以查看是否已设置数据

componentWillMount() {
  // On app load, check if there's existing data. If not, set it.
  chrome.storage.sync.get('allData', (result) => {
    console.log(result.allData);
    //use "in" check as a regular if(result.allData) will
    //return false for empty arrays
    if ( 'allData' in result ) {
      this.setState({ allData });
    } else chrome.storage.sync.set({ allData: [] });
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM