GSI Object Oriented Online Offline (Go4) GO4-6.4.5
Loading...
Searching...
No Matches
cookie.js
Go to the documentation of this file.
1/*!
2 Cookie helper functions
3 Copyright (c) 2023 Dimitri van Heesch
4 Released under MIT license.
5*/
6let Cookie = {
7 cookie_namespace: 'doxygen_',
8
9 readSetting(cookie,defVal) {
10 if (window.chrome) {
11 const val = localStorage.getItem(this.cookie_namespace+cookie) ||
12 sessionStorage.getItem(this.cookie_namespace+cookie);
13 if (val) return val;
14 } else {
15 let myCookie = this.cookie_namespace+cookie+"=";
16 if (document.cookie) {
17 const index = document.cookie.indexOf(myCookie);
18 if (index != -1) {
19 const valStart = index + myCookie.length;
20 let valEnd = document.cookie.indexOf(";", valStart);
21 if (valEnd == -1) {
22 valEnd = document.cookie.length;
23 }
24 return document.cookie.substring(valStart, valEnd);
25 }
26 }
27 }
28 return defVal;
29 },
30
31 writeSetting(cookie,val,days=10*365) { // default days='forever', 0=session cookie, -1=delete
32 if (window.chrome) {
33 if (days==0) {
34 sessionStorage.setItem(this.cookie_namespace+cookie,val);
35 } else {
36 localStorage.setItem(this.cookie_namespace+cookie,val);
37 }
38 } else {
39 let date = new Date();
40 date.setTime(date.getTime()+(days*24*60*60*1000));
41 const expiration = days!=0 ? "expires="+date.toGMTString()+";" : "";
42 document.cookie = this.cookie_namespace + cookie + "=" +
43 val + "; SameSite=Lax;" + expiration + "path=/";
44 }
45 },
46
47 eraseSetting(cookie) {
48 if (window.chrome) {
49 if (localStorage.getItem(this.cookie_namespace+cookie)) {
50 localStorage.removeItem(this.cookie_namespace+cookie);
51 } else if (sessionStorage.getItem(this.cookie_namespace+cookie)) {
52 sessionStorage.removeItem(this.cookie_namespace+cookie);
53 }
54 } else {
55 this.writeSetting(cookie,'',-1);
56 }
57 },
58}