2012-11-14 19 views
6

C'è un modo per aumentare chrome.storage.sync.QUOTA_BYTES_PER_ITEM?Posso aumentare QUOTA_BYTES_PER_ITEM in Chrome?

Per me, il 4096 Byte predefinito è un po 'corto.

ho cercato di eseguire

chrome.storage.sync.QUOTA_BYTES_PER_ITEM = 8192; 

Tuttavia, sembra che il limite effettivo non cambia.

Come posso fare questo?

+0

Nota: dal 1/15/16, il valore (predefinito) di QUOTA_BYTES_PER_ITEM ora è 8K. –

risposta

6

No, QUOTA_BYTES_PER_ITEM è disponibile solo come riferimento; non è un valore impostabile. Si potrebbe utilizzare il valore di QUOTA_BYTES_PER_ITEM di dividere un elemento fino a più elementi, però:

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); 
} 

quindi scrivere una funzione analoga a prendere che recupera dalla chiave e colle l'oggetto di nuovo insieme.

+0

Sembra che ci sia un ciclo infinito nel codice. Aggiorna l'ultima riga del ciclo 'while' a' jsonstr = jsonstr.substr (chrome.storage.sync.QUOTA_BYTES_PER_ITEM); '. –

+0

Avrai anche bisogno di un 'i ++' da qualche parte. –

+0

Si noti inoltre che quando 'chrome.storage.sync.QUOTA_BYTES_PER_ITEM' viene controllato da Chrome, calcola la lunghezza del valore da salvare ma anche la lunghezza di una ** chiave **. Pertanto, è necessario utilizzare qualcosa come 'chrome.storage.sync.QUOTA_BYTES_PER_ITEM - currentKeyLength' quando si utilizza' substr'. –

1

Ho creato un small class per la mia estensione che gestisce il salvataggio e il recupero di stringhe più lunghe di QUOTA_BYTES_PER_ITEM da chrome.storage.sync. Ho basato il mio codice su uno snippet fornito da @apsillers.

Problemi correlati