Guest User

sample code

a guest
Feb 4th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var NUMBER = 0;
  2.  
  3.     function chromeExpansionFn(str){
  4.         return JSON.stringify(JSON.stringify(str));
  5.     }
  6.    
  7.     function syncStore(key, objectToStore, callback) {
  8.         function afterStorageClear(){
  9.             storage.set(storageObj, function(){
  10.                 var lE = chrome.runtime.lastError;
  11.                 if(lE){
  12.                     console.log("error " + lE.message + " received at " + NUMBER);
  13.                     console.log("retrying with NUMBER = " + (NUMBER+=1));
  14.                     syncStore(key, objectToStore, callback);
  15.                 }
  16.                 else {
  17.                     console.log("didn't get any error at NUMBER = " + NUMBER);
  18.                     if(callback) callback();
  19.                 }
  20.             });
  21.         }
  22.        
  23.         var jsonstr = JSON.stringify(objectToStore), i = 0, storageObj = {},
  24.             // (note: QUOTA_BYTES_PER_ITEM only on sync storage)
  25.             // subtract two for the quotes added by stringification
  26.             maxBytesPerItem = storage.QUOTA_BYTES_PER_ITEM - NUMBER,
  27.             // since the key uses up some per-item quota, use
  28.             // "maxValueBytes" to see how much is left for the value
  29.             maxValueBytes, index, segment, counter;
  30.        
  31.         console.log("jsonstr length: " + lengthInUtf8Bytes(jsonstr));
  32.         console.log("after stringification: " + lengthInUtf8Bytes(chromeExpansionFn(jsonstr)));
  33.        
  34.         // split jsonstr into chunks and store them in an object indexed by `key_i`
  35.         while(jsonstr.length > 0) {
  36.             index = key + "_" + i++;
  37.             maxValueBytes = maxBytesPerItem - lengthInUtf8Bytes(index);
  38.            
  39.             counter = maxValueBytes;
  40.             segment = jsonstr.substr(0, counter);
  41.            
  42.             console.log("SEGEMENT BEFORE CHOPPING:");
  43.             //console.log(segment);
  44.             console.log(segment.length);           
  45.             console.log(lengthInUtf8Bytes(chromeExpansionFn(segment)), maxValueBytes);
  46.            
  47.             while(lengthInUtf8Bytes(chromeExpansionFn(segment)) > maxValueBytes)
  48.                 segment = jsonstr.substr(0, --counter);
  49.            
  50.             console.log("SEGEMENT AFTER CHOPPING:");
  51.             console.log(segment.length);
  52.             console.log(lengthInUtf8Bytes(chromeExpansionFn(segment)), maxValueBytes);
  53.            
  54.             storageObj[index] = segment;
  55.             jsonstr = jsonstr.substr(counter);
  56.         }
  57.         // later used by retriever function
  58.         storageObj[key] = i;
  59.         console.log((i + 1) + " keys used (= key + key_i)");
  60.         // say user saves till chunk 20 in case I
  61.         // in case II, user deletes several snippets and brings down
  62.         // total no. of "required" chunks to 15; however, the previous chunks
  63.         // (16-20) remain in memory unless they are "clear"ed.
  64.         storage.clear(afterStorageClear);
  65.     }
Add Comment
Please, Sign In to add comment