Page 1 of 1

Need help with Read/Write from LocalStorage

Posted: Thu Jan 25, 2018 12:36 pm
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)

Re: Need help with Read/Write from LocalStorage

Posted: Fri Jan 26, 2018 7:26 am
by Dirk Geppert
Just discovered: https://github.com/spiderbytes/Preferences
Looks perfect to me :D

Re: Need help with Read/Write from LocalStorage

Posted: Fri Jan 26, 2018 4:07 pm
by IdeasVacuum
But where exactly is the file "Preferences.prefs" stored?

Re: Need help with Read/Write from LocalStorage

Posted: Sat Jan 27, 2018 1:01 pm
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.