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