2019-06-27 05:44:08 +00:00
|
|
|
// +build js,wasm
|
|
|
|
|
|
|
|
package wasm
|
|
|
|
|
|
|
|
import (
|
2019-06-27 22:59:18 +00:00
|
|
|
"strings"
|
2019-06-27 05:44:08 +00:00
|
|
|
"syscall/js"
|
2019-06-27 22:59:18 +00:00
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
2019-06-27 05:44:08 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 22:59:18 +00:00
|
|
|
// StorageKeys returns the list of localStorage keys matching a prefix.
|
|
|
|
func StorageKeys(prefix string) []string {
|
|
|
|
keys := js.Global().Get("Object").Call("keys", js.Global().Get("localStorage"))
|
|
|
|
|
|
|
|
var result []string
|
|
|
|
for i := 0; i < keys.Length(); i++ {
|
|
|
|
value := keys.Index(i).String()
|
|
|
|
if strings.HasPrefix(value, prefix) {
|
|
|
|
result = append(result,
|
|
|
|
strings.TrimPrefix(keys.Index(i).String(), prefix),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Info("LS KEYS: %+v", result)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-06-27 05:44:08 +00:00
|
|
|
// SetSession sets a text value on sessionStorage.
|
|
|
|
func SetSession(key string, value string) {
|
2019-06-27 22:59:18 +00:00
|
|
|
js.Global().Get("localStorage").Call("setItem", key, value)
|
2019-06-27 05:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSession retrieves a text value from sessionStorage.
|
|
|
|
func GetSession(key string) (string, bool) {
|
|
|
|
var value js.Value
|
2019-06-27 22:59:18 +00:00
|
|
|
value = js.Global().Get("localStorage").Call("getItem", key)
|
2019-06-27 05:44:08 +00:00
|
|
|
return value.String(), value.Type() == js.TypeString
|
|
|
|
}
|