f0ckv2/public/s/js/theme.js

66 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2021-04-17 17:55:25 +00:00
const Cookie = {
get: name => {
const c = document.cookie.match(`(?:(?:^|.*; *)${name} *= *([^;]*).*$)|^.*$`)[1];
if(c) return decodeURIComponent(c);
},
set: (name, value, opts = {}) => {
if(opts.days) {
opts['max-age'] = opts.days * 60 * 60 * 24;
delete opts.days;
}
2023-05-06 04:54:09 +00:00
opts.SameSite = 'Strict';
2021-04-17 17:55:25 +00:00
opts = Object.entries(opts).reduce((accumulatedStr, [k, v]) => `${accumulatedStr}; ${k}=${v}`, '');
document.cookie = name + '=' + encodeURIComponent(value) + opts
2021-04-22 02:12:40 +00:00
}
2021-04-17 17:55:25 +00:00
};
2021-04-17 08:43:23 +00:00
(() => {
2021-04-17 17:55:25 +00:00
const acttheme = Cookie.get('theme') ?? "f0ck";
2021-04-22 02:12:40 +00:00
const themecontainer = document.querySelector("li#themes > ul.dropdown-menu");
const themes = [...themecontainer.querySelectorAll("li > a")].map(t => t.innerText.toLowerCase());
if(acttheme !== document.documentElement.getAttribute("theme") && themes.includes(acttheme))
2021-04-17 17:55:25 +00:00
document.documentElement.setAttribute("theme", acttheme);
2021-04-22 02:12:40 +00:00
[...themecontainer.querySelectorAll("li > a")].forEach(t => t.addEventListener("click", e => {
e.preventDefault();
const _theme = e.target.innerText.toLowerCase();
document.documentElement.setAttribute("theme", _theme);
document.querySelector("#themes > a").setAttribute("content", _theme);
Cookie.set("theme", _theme, { path: "/", days: 360 });
return false;
}));
document.addEventListener("keydown", e => {
2021-05-25 12:44:35 +00:00
if(e.target.tagName === "INPUT")
return;
2021-04-22 02:12:40 +00:00
const acttheme = Cookie.get('theme') ?? "f0ck";
const themes = [...themecontainer.querySelectorAll("li > a")].map(t => t.innerText.toLowerCase());
const k = e.key;
if(k === "t") {
e.preventDefault();
let i = themes.indexOf(acttheme);
if(++i >= themes.length)
i = 0;
document.documentElement.setAttribute("theme", themes[i]);
document.querySelector("#themes > a").setAttribute("content", themes[i]);
Cookie.set("theme", themes[i], { path: "/", days: 360 });
}
});
2023-05-06 04:54:09 +00:00
if(tbuttonfull = document.querySelector('svg#a_tfull')) {
tbuttonfull.addEventListener('click', e => {
let f = Cookie.get('fullscreen');
if(f == 1) {
Cookie.set('fullscreen', 0);
document.querySelector('html').setAttribute('res', '');
tbuttonfull.innerHTML = `<use href="/s/img/iconset.svg#window-maximize"></use>`;
}
else {
Cookie.set('fullscreen', 1);
document.querySelector('html').setAttribute('res', 'fullscreen');
tbuttonfull.innerHTML = `<use href="/s/img/iconset.svg#window-minimize"></use>`;
}
return true;
});
}
2021-04-17 08:43:23 +00:00
})();