1

将现有的扩展从 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

}
4

1 回答 1

3

根据扩展 API 路线图 ( https://docs.microsoft.com/en-us/microsoft-edge/extensions/api-support/extension-api-roadmap),Microsoft Edge 支持unlimitedStorage14986 及更高版本的权限(目前仅适用于 Windows 预览体验成员,但在 Windows 10 创意者更新启动后将公开提供)。使用它应该可以让你绕过 1MB 的大小限制,但由于 Edge 的命名空间是browser,你必须将上面的代码更改为browser.storage.local

如果您需要注册 Windows Insider Preview 计划,您可以在此处注册:https ://insider.windows.com

于 2017-03-29T05:27:10.673 回答