将现有的扩展从 chrome 移植到 edge,我遇到了 chrome.storage.local 的 1mb 限制问题。我使用此主题中的函数String length in bytes in JavaScript来查找字符串的大小,并发现如果 json 字符串超过 1mB,它将抛出“超过 1mB”异常,但如果 json 字符串在 250kB 和 1mB 之间,我在 chrome.runtime.lastError 中得到一条消息,说数据超过 1mB,没有抛出异常并且没有存储数据。
我尝试切换到 browser.storage,但得到了相同的结果。这是一个错误还是我使用的字节大小函数错误?
此外,我正在使用 lz-string 压缩数据以尝试增加存储空间。
tmp = JSON.stringify(tmp);
tmpAsString = LZString.compressToUTF16(tmp);
var compresskey = 'compressedJSExtension';
try {
chrome.storage.local.set({[compresskey] : tmp }, function () {
if (chrome.runtime.lastError) console.error(chrome.runtime.lastError);
});
}
catch (f) {
//Handle if the string is over 1mB
}