Вот что получилось:
<head>
<script src="lzwCompress.js"></script>
</head>
<script>
var lzwCompress = window.lzwCompress;
json_path = "data/top8.json";
function compress() {
xttp = new XMLHttpRequest();
xttp.open("GET", json_path);
xttp.send();
xttp.onreadystatechange= function() {
if(this.readyState == 4 && this.status == 200){
var data = JSON.parse(this.responseText);
var compressed = lzwCompress.pack(data);
var compressed = JSON.stringify(compressed)
download(compressed, 'top8_compressed.json', 'utf8');
}
}
}
function decompress() {
xttp = new XMLHttpRequest();
xttp.open("GET", "data/top8_compressed.json");
xttp.send();
xttp.onreadystatechange= function() {
if(this.readyState == 4 && this.status == 200){
var data = JSON.parse(this.responseText);
var original = lzwCompress.unpack(data);
console.log(original);
}
}
}
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
compress();
decompress();
</script>