简体   繁体   中英

Compress large JSON/JS object from content script to background.js - chrome extension

I want to send a large JSON/JS object from content.js to background.js

I have tried the library lz-string to compress the object.

content.js

var compressedJSON = LZString.compress(JSON.stringify(largeObject));
chrome.runtime.sendMessage({type: "type1", result: compressedJSON}, function(response){
  // handle response
});

background.js

var uncompressedJSON = JSON.parse(LZString.decompress(request.result));

Doing so, I get null for uncompressedJSON in background.js

However, if I decompress the same string compressedJSON in content.js using LZString.decompress() , it works!

I wonder if UTF encoding has something to do with this. This library seems to work really well as I am able to compress my object by about 78%.

Suggestions for other libraries are welcome too!

Use LZString.compressToUTF16 and LZString.decompressFromUTF16 :

compressToUTF16 produces "valid" UTF-16 strings in the sense that all browsers can store them safely. So they can be stored in localStorage on all browsers tested (IE9-10, Firefox, Android, Chrome, Safari). Can be decompressed with decompressFromUTF16. This works by using only 15bits of storage per character. The strings produced are therefore 6.66% bigger than those produced by compress

However, it'd be still much much faster to send an uncompressed JSON.stringify result in 32MB string chunks (along with chunk number), then combine them in the background script.

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