Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var NUMBER = 0;
- function chromeExpansionFn(str){
- return JSON.stringify(JSON.stringify(str));
- }
- function syncStore(key, objectToStore, callback) {
- function afterStorageClear(){
- storage.set(storageObj, function(){
- var lE = chrome.runtime.lastError;
- if(lE){
- console.log("error " + lE.message + " received at " + NUMBER);
- console.log("retrying with NUMBER = " + (NUMBER+=1));
- syncStore(key, objectToStore, callback);
- }
- else {
- console.log("didn't get any error at NUMBER = " + NUMBER);
- if(callback) callback();
- }
- });
- }
- var jsonstr = JSON.stringify(objectToStore), i = 0, storageObj = {},
- // (note: QUOTA_BYTES_PER_ITEM only on sync storage)
- // subtract two for the quotes added by stringification
- maxBytesPerItem = storage.QUOTA_BYTES_PER_ITEM - NUMBER,
- // since the key uses up some per-item quota, use
- // "maxValueBytes" to see how much is left for the value
- maxValueBytes, index, segment, counter;
- console.log("jsonstr length: " + lengthInUtf8Bytes(jsonstr));
- console.log("after stringification: " + lengthInUtf8Bytes(chromeExpansionFn(jsonstr)));
- // split jsonstr into chunks and store them in an object indexed by `key_i`
- while(jsonstr.length > 0) {
- index = key + "_" + i++;
- maxValueBytes = maxBytesPerItem - lengthInUtf8Bytes(index);
- counter = maxValueBytes;
- segment = jsonstr.substr(0, counter);
- console.log("SEGEMENT BEFORE CHOPPING:");
- //console.log(segment);
- console.log(segment.length);
- console.log(lengthInUtf8Bytes(chromeExpansionFn(segment)), maxValueBytes);
- while(lengthInUtf8Bytes(chromeExpansionFn(segment)) > maxValueBytes)
- segment = jsonstr.substr(0, --counter);
- console.log("SEGEMENT AFTER CHOPPING:");
- console.log(segment.length);
- console.log(lengthInUtf8Bytes(chromeExpansionFn(segment)), maxValueBytes);
- storageObj[index] = segment;
- jsonstr = jsonstr.substr(counter);
- }
- // later used by retriever function
- storageObj[key] = i;
- console.log((i + 1) + " keys used (= key + key_i)");
- // say user saves till chunk 20 in case I
- // in case II, user deletes several snippets and brings down
- // total no. of "required" chunks to 15; however, the previous chunks
- // (16-20) remain in memory unless they are "clear"ed.
- storage.clear(afterStorageClear);
- }
Add Comment
Please, Sign In to add comment