Need help with Read/Write from LocalStorage

Just starting out? Need help? Post your questions and find answers here.
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Need help with Read/Write from LocalStorage

Post by Dirk Geppert »

Hello, I use the following procedures to save/load cookies.

Code: Select all

Procedure SetCookie(Name.s, Value.s)
  Protected Buffer.s = Name + "=" + Value
  ! document.cookie = v_buffer;
EndProcedure

Procedure.s GetCookie(Name.s)
  ! var value = "; " + document.cookie;
  ! var parts = value.split("; " + v_name + "=");
  ! if (parts.length == 2) return parts.pop().split(";").shift(); 
EndProcedure
Unfortunately, these are lost after closing the browser. That's why I want to use the local storage for storing.
I would like to integrate these localstorage accesses into the code like the cookies.

Code: Select all

  SetLocalStorage (Name.s, Value.s)

  value.s = GetLocalStorage (Name.s)
The help describes how to load and save using a callback. How would it have to be reprogrammed so that it could be done as described above?

Code: Select all

;   SpiderBasic - Persistent file example file

Procedure CreateFileCallback(Status, Filename$, File, SizeRead)
  Select Status
    Case #PB_Status_Saved
      Debug "File saved: " + Filename$
      Debug "Please relaunch the program to test the persitency."
     
    Case #PB_Status_Error
      Debug "Can't save the file: " + Filename$
  EndSelect
EndProcedure


Procedure ReadFileCallback(Status, Filename$, File, SizeRead)
  Select Status
    Case #PB_Status_Loaded
      Debug "File found in localstorage: " + Filename$
      Debug "Displaying file content: "
      
      ; Read the file content as string
      Debug ReadString(File, #PB_File_IgnoreEOL)
      CloseFile(File)
     
    Case #PB_Status_Error
      Debug "File not found in localstorage: " + Filename$
      Debug "Creating a new file..."
     
      If CreateFile(0, "test.txt", @CreateFileCallback(), #PB_LocalStorage)
        WriteString(0, "Hello world !" + #LF$  + "Second line.")
        CloseFile(0)
      Else
        Debug "CreateFile() failed"
      EndIf
  EndSelect
EndProcedure

; Try to read the file if already present, or it will create a new one
;
ReadFile(0, "test.txt", @ReadFileCallback(), #PB_LocalStorage)
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Re: Need help with Read/Write from LocalStorage

Post by Dirk Geppert »

Just discovered: https://github.com/spiderbytes/Preferences
Looks perfect to me :D
IdeasVacuum
Posts: 143
Joined: Tue Feb 25, 2014 1:27 pm

Re: Need help with Read/Write from LocalStorage

Post by IdeasVacuum »

But where exactly is the file "Preferences.prefs" stored?
tj1010
Posts: 201
Joined: Wed May 27, 2015 1:36 pm
Contact:

Re: Need help with Read/Write from LocalStorage

Post by tj1010 »

HTML5 localstorage has a small size limit and can be wiped with privacy and cleaner solutions both integrated and third-party.

Just use a Cordova plugin that uses native file system API. Unless you're making a web hosted product.
Post Reply