diff options
author | Frankie B <git@diskfloppy.me> | 2024-06-11 18:02:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-11 18:02:01 +0100 |
commit | 0f52d80ca67a49258b235f5831163dd72fbd54cf (patch) | |
tree | 9c5cd36b6e0a233e09ac88a4409fb68c63e4781a /resources/js/schemeSwap.js | |
parent | a64bcc2c4639d5804b6dada23151bfcb8b198121 (diff) |
Merge MVC rewrite into master (#21)
* Just commit it all
* Require auth
* crap
* Update homepage
* Block AI scrapers
* Update cache update script
* Add dummy file
* Remove unnecessary lastfm config var
* Use withQueryParameters for LastFM API
* Fix embeds
* Update example env
* Smard
Diffstat (limited to 'resources/js/schemeSwap.js')
-rw-r--r-- | resources/js/schemeSwap.js | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/resources/js/schemeSwap.js b/resources/js/schemeSwap.js new file mode 100644 index 0000000..3baa09b --- /dev/null +++ b/resources/js/schemeSwap.js @@ -0,0 +1,70 @@ +/** + * Retrieves a cookies value + * @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]; + while (c.charAt(0) === ' ') { + c = c.substring(1); + } + if (c.indexOf(name) === 0) { + 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(); + document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/;SameSite=Strict;Domain=.diskfloppy.me"; +} + +/** + * 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 + * @param {string} scheme Color scheme ID + */ +function swapScheme(scheme) { + setCookie("colorscheme", scheme, 90); + document.getElementById("css-colorscheme").href = `/css/colorschemes/${scheme}.css`; + console.log(`Set colorscheme to ${getCookie("colorscheme")}`) +} + +function setSchemeSelector() { + if (!cookieExists("colorscheme")) { + setCookie("colorscheme", "catppuccin-macchiato", 90); + } else { + const scheme = getCookie("colorscheme"); + const scheme_selector = document.getElementById("scheme-selector"); + if (scheme && scheme_selector) { + for (let option of scheme_selector.options) { + if (option.value === scheme) { + option.selected = true; + break; + } + } + } + } +} |