-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplePersistentData.min.js
More file actions
5 lines (5 loc) · 1.42 KB
/
Copy pathsimplePersistentData.min.js
File metadata and controls
5 lines (5 loc) · 1.42 KB
1
2
3
4
5
function save_data(){if(localStorage.getItem("DEVMODE")){localStorage.setItem("app_data",JSON.stringify(data))}else{fileWrapper.write("app_data.txt",JSON.stringify(data),function(){console.log("Wrote Data")},function(){console.log("Failed to Write Data")})}}
function load_data(success_callback,error_callback){if(localStorage.getItem("DEVMODE")){if(localStorage.getItem("app_data")!==null){data=JSON.parse(localStorage.getItem("app_data"))}
success_callback()}else{fileWrapper.read("app_data.txt",function(d){data=JSON.parse(d);success_callback()},function(){data="";error_callback()})}}
window.fileWrapper={write:function(name,data,success,fail){var gotFileSystem=function(fileSystem){fileSystem.root.getFile(name,{create:!0,exclusive:!1},gotFileEntry,fail)};var gotFileEntry=function(fileEntry){fileEntry.createWriter(gotFileWriter,fail)};var gotFileWriter=function(writer){writer.onwrite=success;writer.onerror=fail;writer.write(data)};window.requestFileSystem(window.LocalFileSystem.PERSISTENT,data.length||0,gotFileSystem,fail)},read:function(name,success,fail){var gotFileSystem=function(fileSystem){fileSystem.root.getFile(name,{create:!1,exclusive:!1},gotFileEntry,fail)};var gotFileEntry=function(fileEntry){fileEntry.file(gotFile,fail)};var gotFile=function(file){reader=new FileReader();reader.onloadend=function(){success(this.result)};reader.readAsText(file)}
window.requestFileSystem(window.LocalFileSystem.PERSISTENT,0,gotFileSystem,fail)}}