Skip to content

Commit

Permalink
Merge pull request #4085 from IgorA100/patch-152
Browse files Browse the repository at this point in the history
Feat: Added the ability to save objects and arrays in cookies using JSON (skin.js)
  • Loading branch information
connortechnology committed Jul 4, 2024
2 parents 1874c2b + 87d31a1 commit a88065f
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions web/skins/classic/js/skin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit a88065f

Please sign in to comment.