简体   繁体   中英

Chrome API: Runtime QUOTA_BYTES_PER_ITEM quota exceeded Error, but precheck passes

I am getting a QUOTA_BYTES_PER_ITEM quota exceeded error when trying to save an object to storage, but my precheck of the size passes. I am sure I am making some sort of basic mistake here (is this a valid way to check size of an object?). I have already compressed the item I want to save with LZString, but regardless, it seems much smaller than the quota.

var objToSave = {};   
objToSave[myKey] = compressedObj;  

console.log("Size of obj is: " + JSON.stringify(objToSave).length); //prints 3452
console.log(chrome.storage.sync.QUOTA_BYTES_PER_ITEM); //prints 8192
if (JSON.stringify(objToSave).length >= (chrome.storage.sync.QUOTA_BYTES_PER_ITEM)) { // this never triggers
      alert('objToSave is too large!');  
      return;   
}

chrome.storage.sync.set(objToSave, function() {    
  if (chrome.runtime.lastError) { // this error gets triggered.
    console.log("Error: " + chrome.runtime.lastError.message); // this error gets triggered. 
    return customAlert("Error!: " + chrome.runtime.lastError.message);    
  }   
});

Thank you @nishant and @wOxxOm - that is exactly the error. I was checking the size incorrectly.

To correctly check the size, I am now using to get the byte size vs just the length of the string

var s = JSON.stringify(objToSave);
encodeURI(s).split(/%(?:u[0-9A-F]{2})?[0-9A-F]{2}|./).length-1)

which is giving me a size of 10765 which I am now breaking down further using @wOxxOm's answer on https://stackoverflow.com/a/67429150/4797507

Well, the only logical reason for you to get QUOTA_BYTES_PER_ITEM is that you are trying to use a string that exceeds that 8k mark...

However there are 2 things that I feel might be going wrong here..

  1. Double-check the key:value pair
  2. "variable byte length" Since you are encoding the string there might be a slight chance that your string is full of 2-byte data [if the character code is bigger than 256 or (<= 0xFF) then the byte size is 2, Your string might be of 3452 but full of 2 bytes then it would cross 8k mark, it might just be that blind spot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM