LocalStorage (html5)

olive

Member
3rd Party Module Dev
Tutorial Author
Patreon Bronze
Joined
Jul 17, 2017
Messages
77
I've put together a (very) small module that gives access to saving and loading from LocalStorage on the html5 target. It's similar to the existing SaveState, but you can specify your own key/value pairs instead of needing to have it be one long string. It's about as close as you can get to being able to save things to disk for html5.

Repo with example: https://github.com/wickworks/localstorage

localstorage.cxs
Code:
#If TARGET="html5"
    Import "native/localstorage.js"
#Else
    #Error "The localstorage module is only available on the html5 target"
#Endif

Extern

Function SaveLocalStorage(key:String, value:String)
Function LoadLocalStorage(key:String)

native/localstorage.js
Code:
SaveLocalStorage = function(key, value) {
    localStorage.setItem(key, value);    //key can't start with dot in Chrome!
    return 1;
}

LoadLocalStorage = function(key) {
    var state = localStorage.getItem(key);
    if (state) return state;
    return "";
}

(This is the first time I've managed to get Extern working to run javascript code! It wasn't as hard as I was afraid it would be.)
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,136
Thanks. You can also do this. This will make you a download file.
Chrome will block this of course you have to accept that files are allowed to save to your device.

Save file on HTML5 platform :

savefile.cxs
Code:
' A simple way to save/download a string on HTML5 target

Import mojo2

Import "commands.js"
Extern
Function SaveString(content:String)
Public

Function Main()
    New MyApp
End

Global canvas:Canvas

Class MyApp Extends App

    Method OnCreate()
        canvas=New Canvas
        SetUpdateRate 0
        SaveString "It works!"
    End Method
   
    Method OnUpdate()
        If MouseHit(0) Then SaveString "It works!"
    End Method
   
    Method OnRender()
        canvas.Clear
        canvas.Flush
    End
   
End Class

commands.js
Code:
function SaveString(content){
    uriContent = "data:application/octet-stream," + encodeURIComponent(content);
    window.open(uriContent,"Download File");
}
 

olive

Member
3rd Party Module Dev
Tutorial Author
Patreon Bronze
Joined
Jul 17, 2017
Messages
77
Neat! I didn't know that was an option. Do you have a corresponding solution for being able to let users upload files to the browser & access that data in cerberus?

LocalStorage is nice because you can save/load without user input (I'm now using it for game options, where I don't necessarily want to bother folks by forcing them to manually download/upload a preferences file).
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,136
Neat! I didn't know that was an option. Do you have a corresponding solution for being able to let users upload files to the browser & access that data in cerberus?

LocalStorage is nice because you can save/load without user input (I'm now using it for game options, where I don't necessarily want to bother folks by forcing them to manually download/upload a preferences file).

I do, I think I should give myself some time and dig it up at some point. It would be handy to have.
I'll give you a message as soon as I have it ready.
 
Top Bottom