LocalStorage, SessionStorage

Share your advanced knowledge/code with the community.
User avatar
eddy
Posts: 124
Joined: Thu Mar 27, 2014 8:34 am

LocalStorage, SessionStorage

Post by eddy »

- web storages (Local Storage & Session Storage) have bigger capacity.
- local storage is usefull if you code an "offline" mode
- session storage is like bigger domain-restricted cookie
- Attention, web storage data are not crypted. (e.g. players can read/write their scores stored in web storage.)

Code: Select all

Import ""
  SetLocalStorage(Key.s, Value.f) As "localStorage.setItem"
  GetLocalStorage.f(Key.s) As "localStorage.getItem"
  SetLocalStorageString(Key.s, Value.s) As "localStorage.setItem"
  RemoveLocalStorage(Key.s) As "localStorage.removeItem"
  ClearLocalStorage() As "localStorage.clear"
  
  SetSessionStorage(Key.s, Value.f) As "sessionStorage.setItem"
  GetSessionStorage.f(Key.s) As "sessionStorage.getItem"
  SetSessionStorageString(Key.s, Value.s) As "sessionStorage.setItem"
  RemoveSessionStorage(Key.s) As "sessionStorage.removeItem"
  ClearSessionStorage() As "sessionStorage.clear"
EndImport 

Procedure.s GetLocalStorageString(Key.s) 
  !return localStorage.getItem(v_Key) || '';
EndProcedure
Procedure.s GetSessionStorageString(Key.s) 
  !return sessionStorage.getItem(v_Key) || '';
EndProcedure

Procedure GetLocalStorageKeys(List KeyList.s()) 
  ClearList(KeyList())
  !for(var i=0;i<localStorage.length;i++){
    Protected key.s
    !v_key=localStorage.key(i);
    AddElement(KeyList())
    KeyList()=key
  !} //next 
EndProcedure
Procedure.s GetSessionStorageKeys(List KeyList.s()) 
  ClearList(KeyList())
  !for(var i=0;i<sessionStorage.length;i++){
    Protected key.s
    !v_key=sessionStorage.key(i);
    AddElement(KeyList())
    KeyList()=key
  !} //next 
EndProcedure

Procedure.i FindLocalStorage(Key.s)
  !if (v_Key in localStorage) return 1;
  EndProcedure
  Procedure.i FindSessionStorage(Key.s)
  !if (v_Key in sessionStorage) return 1;
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  ; *******************************
  ; EXAMPLE 
  ; *******************************
  
  ;local data will persist even after the browser is closed
  If FindLocalStorage("localData")
    Debug "localData exists!"+#LF$+"Get localData = "+GetLocalStorageString("localData")+#LF$
  Else 
    SetLocalStorageString("localData","my string value")
    Debug "Set localData:"+GetLocalStorageString("localData")+#LF$
  EndIf 
  
  ;session data is also persistent data, however the data is tied to a browsing context (i.e. a browser tab Or window)
  If FindSessionStorage("sessionData")
    Debug "sessionData exists!"+#LF$+"Get sessionData = "+GetSessionStorage("sessionData")+#LF$
  Else 
    SetSessionStorage("sessionData",1234)
    Debug "Set sessionData:"+GetSessionStorage("sessionData")+#LF$
  EndIf 
  
  NewList Key$()
  GetLocalStorageKeys(Key$())
  ForEach Key$() : Debug "key:"+Key$() : Next
  GetSessionStorageKeys(Key$())
  ForEach Key$() : Debug "key:"+Key$() : Next
CompilerEndIf 
bbanelli
Posts: 107
Joined: Mon Jul 13, 2015 7:40 am

Re: LocalStorage, SessionStorage

Post by bbanelli »

Greetings, I'm getting:

Code: Select all

Uncaught ReferenceError: v_Key is not defined
in this code:

Code: Select all

function f_findlocalstorage(v_key) {
if (v_Key in localStorage) return 1;
return 0;
}
In Chrome 53.0.2785.143 m (64-bit).

Any clues, I'm kinda lost in this JS thing... :)
"If you lie to the compiler, it will get its revenge."
Henry Spencer
http://www.pci-z.com/
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: LocalStorage, SessionStorage

Post by Peter »

change all v_* - Variables to lowercase:

Code: Select all

if (v_key in localStorage) return 1;
Greetings ... Peter
bbanelli
Posts: 107
Joined: Mon Jul 13, 2015 7:40 am

Re: LocalStorage, SessionStorage

Post by bbanelli »

Hi Peter, thank you very much.

So, "EnableExplicit" has no use in SB for such cases?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
http://www.pci-z.com/
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: LocalStorage, SessionStorage

Post by Peter »

bbanelli wrote:So, "EnableExplicit" has no use in SB for such cases?
yes, EnableExplicit is for SpiderBasic-Code only (and not for JavaScript-Code).

Greetings ... Peter
bbanelli
Posts: 107
Joined: Mon Jul 13, 2015 7:40 am

Re: LocalStorage, SessionStorage

Post by bbanelli »

Peter wrote:
bbanelli wrote:So, "EnableExplicit" has no use in SB for such cases?
yes, EnableExplicit is for SpiderBasic-Code only (and not for JavaScript-Code).

Code: Select all

Define.s OS, Browser, txt
! v_os = navigator.platform;
! v_browser = navigator.userAgent;
However, this code makes no difference whether SB's variables are case (in)sensitive?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
http://www.pci-z.com/
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: LocalStorage, SessionStorage

Post by Peter »

* SpiderBasic-Variables are always case-insensitive (HelloWorld = helloWORLD)
* JavaScript-Variables are always case-sensitive (HelloWorld <> helloWORLD)

And EnableExplicit considered only SpiderBasic-Variables.

Fred decides to rename all SpiderBasic-Variables to v_[LowerCaseVariableName] when transpiling into JavaScript.

So HelloWorld in SpiderBasic turns to v_helloworld in JavaScript

Greetings ... Peter
Post Reply