diff options
author | FChannel0 <77419041+FChannel0@users.noreply.github.com> | 2021-08-20 01:35:26 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-20 01:35:26 -0700 |
commit | 502558e0bdaf67ca4c012fc1983f42430b8854cd (patch) | |
tree | d66b015274b962fedb28c52c0efaaa068368f2b1 /static/js/themes.js | |
parent | 21917eb2ea72fe40d70c2c991e821aace5430c23 (diff) | |
parent | a497499817cbf72b295253e1a2bb1011d121ba28 (diff) |
Merge pull request #52 from KushBlazingJudah/development
Themes
Diffstat (limited to 'static/js/themes.js')
-rw-r--r-- | static/js/themes.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/static/js/themes.js b/static/js/themes.js new file mode 100644 index 0000000..3f1b906 --- /dev/null +++ b/static/js/themes.js @@ -0,0 +1,40 @@ +function setCookie(key, value, age) { + document.cookie = key + "=" + encodeURIComponent(value) + ";sameSite=strict;max-age=" + 60 * 60 * 24 * age + ";path=/"; +} + +function getCookie(key) { + if (document.cookie.length != 0) { + return document.cookie.split('; ').find(row => row.startsWith(key)).split('=')[1]; + } + return ""; +} + +function setTheme(name) { + for (let i = 0, tags = document.getElementsByTagName("link"); i < tags.length; i++) { + if (tags[i].type === "text/css" && tags[i].title) { + tags[i].disabled = !(tags[i].title === name); + } + } + + setCookie("theme", name, 3650); +} + +function applyTheme() { + // HACK: disable all of the themes first. this for some reason makes things work. + for (let i = 0, tags = document.getElementsByTagName("link"); i < tags.length; i++) { + if (tags[i].type === "text/css" && tags[i].title) { + tags[i].disabled = true; + } + } + let theme = getCookie("theme") || "default"; + setTheme(theme); + + // reflect this in the switcher + let switcher = document.getElementById("themeSwitcher"); + for(var i = 0; i < switcher.options.length; i++) { + if (switcher.options[i].value === theme) { + switcher.selectedIndex = i; + break; + } + } +} |