2024-01-22 00:09:48 +00:00
|
|
|
/**
|
2024-01-22 01:16:42 +00:00
|
|
|
* Retrieves a cookies value
|
2024-01-22 00:09:48 +00:00
|
|
|
* @param {string} cname Cookie name
|
|
|
|
* @returns {string} Cookie value
|
|
|
|
*/
|
|
|
|
function getCookie(cname) {
|
|
|
|
let name = cname + "=";
|
|
|
|
let decodedCookie = decodeURIComponent(document.cookie);
|
|
|
|
let ca = decodedCookie.split(';');
|
|
|
|
for(let i = 0; i <ca.length; i++) {
|
|
|
|
let c = ca[i];
|
2024-01-26 12:26:48 +00:00
|
|
|
while (c.charAt(0) === ' ') {
|
2024-01-22 00:09:48 +00:00
|
|
|
c = c.substring(1);
|
|
|
|
}
|
2024-01-26 12:26:48 +00:00
|
|
|
if (c.indexOf(name) === 0) {
|
2024-01-22 00:09:48 +00:00
|
|
|
return c.substring(name.length, c.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets/creates a cookie
|
|
|
|
* @param {string} cname Cookie name
|
|
|
|
* @param {string} cvalue Cookie value
|
|
|
|
* @param {number} exdays Cookie lifespan (days)
|
|
|
|
*/
|
|
|
|
function setCookie(cname, cvalue, exdays) {
|
|
|
|
const d = new Date();
|
|
|
|
d.setTime(d.getTime() + (exdays*24*60*60*1000));
|
|
|
|
let expires = "expires="+ d.toUTCString();
|
2024-01-26 12:28:50 +00:00
|
|
|
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/;SameSite=Strict;Domain=.diskfloppy.me";
|
2024-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a cookie exists
|
|
|
|
* @param {string} cname Cookie name
|
|
|
|
* @returns {boolean} If cookie exists or not
|
|
|
|
*/
|
|
|
|
function cookieExists(cname) {
|
|
|
|
const cvalue = getCookie(cname);
|
|
|
|
return cvalue !== "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Swaps the colorscheme
|
2024-01-22 01:16:42 +00:00
|
|
|
* @param {string} scheme Color scheme ID
|
2024-01-22 00:09:48 +00:00
|
|
|
*/
|
2024-07-12 15:58:31 +00:00
|
|
|
function swapScheme() {
|
|
|
|
let scheme = document.getElementById('scheme-selector').value ;
|
2024-01-22 00:09:48 +00:00
|
|
|
setCookie("colorscheme", scheme, 90);
|
|
|
|
document.getElementById("css-colorscheme").href = `/css/colorschemes/${scheme}.css`;
|
|
|
|
console.log(`Set colorscheme to ${getCookie("colorscheme")}`)
|
|
|
|
}
|
2024-01-22 01:33:40 +00:00
|
|
|
|
|
|
|
function setSchemeSelector() {
|
|
|
|
if (!cookieExists("colorscheme")) {
|
|
|
|
setCookie("colorscheme", "catppuccin-macchiato", 90);
|
|
|
|
} else {
|
|
|
|
const scheme = getCookie("colorscheme");
|
2024-01-26 12:26:48 +00:00
|
|
|
const scheme_selector = document.getElementById("scheme-selector");
|
|
|
|
if (scheme && scheme_selector) {
|
|
|
|
for (let option of scheme_selector.options) {
|
|
|
|
if (option.value === scheme) {
|
2024-01-22 01:33:40 +00:00
|
|
|
option.selected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|