【发布时间】:2015-02-11 22:01:13
【问题描述】:
如果用户将数据添加到扩展程序chrome.storage.local/sync,则扩展程序会更新,因为发布了新版本。 chrome.storage.local/sync 会保持不变还是会重置?
【问题讨论】:
标签: google-chrome google-chrome-extension
如果用户将数据添加到扩展程序chrome.storage.local/sync,则扩展程序会更新,因为发布了新版本。 chrome.storage.local/sync 会保持不变还是会重置?
【问题讨论】:
标签: google-chrome google-chrome-extension
不,还是一样。
实际上,如果您的数据格式因更新而发生变化,您可以在存储中使用一些“模式版本”变量来升级存储。
但是,请务必记住,卸载扩展程序将彻底清除存储空间,包括如果启用了同步,sync 存储空间。
【讨论】:
storage.sync 将继续存在。众多优势之一..
如果更新扩展时未调用chrome.storage.sync.set,则保留旧数据。
如果状态键值在更新过程中发生变化,需要清除数据,否则会积累大量丢弃的值。
const initData = { a: 10, b: 20 };
// Use old value if there is one
// If a new value is added, the value of `initData` is used by default
chrome.storage.sync.get(dataRaw => {
const data = Object.entries(initData).reduce((acc, [k, v]) => {
return { ...acc, [k]: dataRaw[k] === undefined ? v : dataRaw[k] };
}, {});
// Clean up unwanted data
chrome.storage.sync.clear(() => chrome.storage.sync.set(data));
});
【讨论】: