summaryrefslogtreecommitdiff
path: root/js/utility.js
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2020-01-19 10:51:08 +0300
committerAndrew Dolgov <[email protected]>2020-01-19 10:51:08 +0300
commitaa56bcaf441ca59f94c8529018ebb4e94844d1d6 (patch)
treeaf7c3a2110f2f64647f0312191cd0837ad7c3762 /js/utility.js
parent303f8fb329ff75e41559806c9affd8c3c49504b0 (diff)
support night mode when using share by URL
Diffstat (limited to 'js/utility.js')
-rw-r--r--js/utility.js39
1 files changed, 39 insertions, 0 deletions
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();
+});