using local filestorage for Game save - jackdarker/TwineTest GitHub Wiki

Using the FileSaver.js, I'm able to write the Json to disk. The reading is an async operation done with the default FileReader-API.
I extended the window.storage-object with the following functions:
compressLocalSave: false, //this is for debugging (save file uncompressed)
loadFile: function(input){
let file = input.files[0];
let fileReader = new FileReader();
fileReader.onload = function() {
window.storage.rebuildFromSave(fileReader.result,window.storage.compressLocalSave);
div = document.querySelector('#backdrop'); //see save/load dialog
div.parentNode.removeChild(div);
};
fileReader.onerror = function() {
alert(fileReader.error);
};
fileReader.readAsText(file);
return(true);
},
saveFile: function(){
var hash = JSON.stringify({state:window.story.state,
history:window.story.history,checkpointName:window.story.checkpointName});
if(window.storage.compressLocalSave) hash = LZString.compressToBase64(hash);
var filename= window.story.name+"_Save.dat";
var blob = new Blob([hash], {type: "text/plain;charset=utf-8"});
saveAs(blob, filename); //FileSaver API
},