diff --git a/web/skins/classic/js/skin.js b/web/skins/classic/js/skin.js index c1f27d7e84..2e2a82fa70 100644 --- a/web/skins/classic/js/skin.js +++ b/web/skins/classic/js/skin.js @@ -761,7 +761,19 @@ 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 = (typeof value === 'string') ? value : JSON.stringify(value); let expires = ""; if (seconds) { const date = new Date(); @@ -771,18 +783,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 or object 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) {