From aa56bcaf441ca59f94c8529018ebb4e94844d1d6 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 19 Jan 2020 10:51:08 +0300 Subject: support night mode when using share by URL --- js/utility.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 js/utility.js (limited to 'js/utility.js') diff --git a/js/utility.js b/js/utility.js new file mode 100644 index 000000000..899dbeb02 --- /dev/null +++ b/js/utility.js @@ -0,0 +1,39 @@ +Event.observe(window, "load", function() { + const UtilityJS = { + apply_night_mode: function (is_night, link) { + console.log("night mode changed to", is_night); + + if (link) { + const css_override = is_night ? "themes/night.css" : "css/default.css"; + + link.setAttribute("href", css_override + "?" + Date.now()); + } + }, + setup_night_mode: function() { + const mql = window.matchMedia('(prefers-color-scheme: dark)'); + + const link = new Element("link", { + rel: "stylesheet", + id: "theme_auto_css" + }); + + link.onload = function() { + document.querySelector("body").removeClassName("css_loading"); + }; + + try { + mql.addEventListener("change", () => { + UtilityJS.apply_night_mode(mql.matches, link); + }); + } catch (e) { + console.warn("exception while trying to set MQL event listener"); + } + + document.querySelector("head").appendChild(link); + + UtilityJS.apply_night_mode(mql.matches, link); + } + }; + + UtilityJS.setup_night_mode(); +}); -- cgit v1.2.3