我可以在Chrome中增加QUOTA_BYTES_PER_ITEM吗?

Pen*_*gin 7 google-chrome google-chrome-extension

有没有办法增加chrome.storage.sync.QUOTA_BYTES_PER_ITEM?

对我来说,默认的4096字节有点短.

我试图执行

chrome.storage.sync.QUOTA_BYTES_PER_ITEM = 8192;
Run Code Online (Sandbox Code Playgroud)

但是,似乎实际限制不会改变.

我怎样才能做到这一点?

aps*_*ers 8

不,QUOTA_BYTES_PER_ITEM仅供参考; 它不是可设定的值.您可以使用值QUOTA_BYTES_PER_ITEM将项目拆分为多个项目,但是:

function syncStore(key, objectToStore, callback) {
    var jsonstr = JSON.stringify(objectToStore);
    var i = 0;
    var storageObj = {};

    // split jsonstr into chunks and store them in an object indexed by `key_i`
    while(jsonstr.length > 0) {
        var index = key + "_" + i++;

        // since the key uses up some per-item quota, see how much is left for the value
        // also trim off 2 for quotes added by storage-time `stringify`
        var valueLength = chrome.storage.sync.QUOTA_BYTES_PER_ITEM - index.length - 2;

        // trim down segment so it will be small enough even when run through `JSON.stringify` again at storage time
        var segment = jsonstr.substr(0, valueLength);           
        while(JSON.stringify(segment).length > valueLength)
            segment = jsonstr.substr(0, --valueLength);

        storageObj[index] = segment;
        jsonstr = jsonstr.substr(valueLength);
    }

    // store all the chunks
    chrome.storage.sync.set(storageObj, callback);
}
Run Code Online (Sandbox Code Playgroud)

然后编写一个类似的获取函数,该函数通过键获取并将对象粘合在一起.