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

Need help with Read/Write from LocalStorage

by Dirk Geppert Thu Jan 25, 2018 12:36 pm

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: 45
Joined: Fri Sep 22, 2017 7:02 am

Re: Need help with Read/Write from LocalStorage

by Dirk Geppert Fri Jan 26, 2018 7:26 am

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

Re: Need help with Read/Write from LocalStorage

by IdeasVacuum Fri Jan 26, 2018 4:07 pm

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

Re: Need help with Read/Write from LocalStorage

by tj1010 Sat Jan 27, 2018 1:01 pm

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.
Return to Coding Questions

Who is online

Users browsing this forum: No registered users and 2 guests