From afeddf57f5d6b3ba6443dbc5e3a7648b6d4b4c27 Mon Sep 17 00:00:00 2001 From: IgorA100 Date: Thu, 4 Jul 2024 16:35:22 +0300 Subject: [PATCH 1/4] Added the ability to save objects and arrays in cookies using JSON (skin.js) --- web/skins/classic/js/skin.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/web/skins/classic/js/skin.js b/web/skins/classic/js/skin.js index c1f27d7e84..9505f0454a 100644 --- a/web/skins/classic/js/skin.js +++ b/web/skins/classic/js/skin.js @@ -761,7 +761,24 @@ function setButtonState(element_id, btnClass) { } } +function isJSON (str) { + if (typeof str !== 'string') return false; + try { + const result = JSON.parse(str); + const type = Object.prototype.toString.call(result); + return type === '[object Object]' || type === '[object Array]'; // We only pass objects and arrays + } catch (e) { + return false; // This is also not JSON + } +}; + function setCookie(name, value, seconds) { + var newValue = ''; + if (typeof value === 'string') { + var newValue = value; + } else { + var newValue = JSON.stringify(value); + } let expires = ""; if (seconds) { const date = new Date(); @@ -771,18 +788,28 @@ function setCookie(name, value, seconds) { // 2147483647 is 2^31 - 1 which is January of 2038 to avoid the 32bit integer overflow bug. expires = "; max-age=2147483647"; } - document.cookie = name + "=" + (value || "") + expires + "; path=/; samesite=strict"; + document.cookie = name + "=" + (newValue || "") + expires + "; path=/; samesite=strict"; } +/** +* If JSON is stored in cookies, the function will return an array of values. +*/ function getCookie(name) { var nameEQ = name + "="; + var result = null; var ca = document.cookie.split(';'); for (var i=0; i < ca.length; i++) { + if (result) break; var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1, c.length); - if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); + if (c.indexOf(nameEQ) == 0) { + result = c.substring(nameEQ.length, c.length); + break; + } } - return null; + if (isJSON(result)) result = JSON.parse(result); + + return result; } function delCookie(name) { From 415e5e9f0a03cd966445cb36965f2666678d0c36 Mon Sep 17 00:00:00 2001 From: IgorA100 Date: Thu, 4 Jul 2024 16:40:17 +0300 Subject: [PATCH 2/4] Code optimization (skin.js) --- web/skins/classic/js/skin.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/web/skins/classic/js/skin.js b/web/skins/classic/js/skin.js index 9505f0454a..6f0b2cb21f 100644 --- a/web/skins/classic/js/skin.js +++ b/web/skins/classic/js/skin.js @@ -773,12 +773,7 @@ function isJSON (str) { }; function setCookie(name, value, seconds) { - var newValue = ''; - if (typeof value === 'string') { - var newValue = value; - } else { - var newValue = JSON.stringify(value); - } + var newValue = (typeof value === 'string') ? value : JSON.stringify(value); let expires = ""; if (seconds) { const date = new Date(); From 837b17cc4e305e13ab45ad371fe46029750fa988 Mon Sep 17 00:00:00 2001 From: IgorA100 Date: Thu, 4 Jul 2024 16:45:23 +0300 Subject: [PATCH 3/4] Chore: eslint (skin.js) --- web/skins/classic/js/skin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/skins/classic/js/skin.js b/web/skins/classic/js/skin.js index 6f0b2cb21f..325f156743 100644 --- a/web/skins/classic/js/skin.js +++ b/web/skins/classic/js/skin.js @@ -761,7 +761,7 @@ function setButtonState(element_id, btnClass) { } } -function isJSON (str) { +function isJSON(str) { if (typeof str !== 'string') return false; try { const result = JSON.parse(str); @@ -786,7 +786,7 @@ function setCookie(name, value, seconds) { document.cookie = name + "=" + (newValue || "") + expires + "; path=/; samesite=strict"; } -/** +/* * If JSON is stored in cookies, the function will return an array of values. */ function getCookie(name) { From 87d31a1d776cee619fc8e90a1faa8cf9c983bc80 Mon Sep 17 00:00:00 2001 From: IgorA100 Date: Thu, 4 Jul 2024 17:03:53 +0300 Subject: [PATCH 4/4] Updated comment (skin.js) --- web/skins/classic/js/skin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/skins/classic/js/skin.js b/web/skins/classic/js/skin.js index 325f156743..2e2a82fa70 100644 --- a/web/skins/classic/js/skin.js +++ b/web/skins/classic/js/skin.js @@ -787,7 +787,7 @@ function setCookie(name, value, seconds) { } /* -* If JSON is stored in cookies, the function will return an array of values. +* If JSON is stored in cookies, the function will return an array or object of values. */ function getCookie(name) { var nameEQ = name + "=";