From f02b06329076b6efbc8f2463179f39390ed9c262 Mon Sep 17 00:00:00 2001 From: Toby Simmons Date: Thu, 10 Feb 2022 22:33:36 +0000 Subject: [PATCH] first commit --- README.md | 0 init.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ init.php | 27 +++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 README.md create mode 100644 init.js create mode 100644 init.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/init.js b/init.js new file mode 100644 index 0000000..d46d105 --- /dev/null +++ b/init.js @@ -0,0 +1,53 @@ +require(['dojo/_base/kernel', 'dojo/ready'], function (dojo, ready) { + ready(function () { + PluginHost.register(PluginHost.HOOK_INIT_COMPLETE, () => { + + App.hotkey_actions["toggle_night_mode"] = function () { + App.toggleNightMode() + }; + + App.toggleNightMode = function () { + const link = App.byId("theme_css"); + if (!link) return; + + let user_css = false; + let href = link.getAttribute("href").split("?")[0]; + let userCSS = function(url, or = false) { + dojo.xhrGet({ + url: url, + handleAs: 'text', + sync: true, + preventCache: true, + load: function() { user_css = url }, + error: function() { user_css = or } + }); + } + + if (href.indexOf("themes.local/") > -1) { + if (href.indexOf("_night.css") > -1) + userCSS(href.replace("_night.css", ".css")); + else + userCSS(href.replace(".css", "_night.css")); + } else if (href.indexOf("/night.css") > -1) + userCSS("themes/light.css", "css/default.css"); + else + userCSS("themes/night.css"); + + if (user_css) { + App.byId("main").fadeOut(); + setTimeout(() => { + link.setAttribute("href", user_css); + xhrPost("backend.php", { + op: "rpc", + method: "setpref", + key: "USER_CSS_THEME", + value: user_css.split("/")[1] + }); + setTimeout(() => { App.byId("main").fadeIn() }, 200); + }, 300); + } + }; + + }); + }); +}); diff --git a/init.php b/init.php new file mode 100644 index 0000000..53e20ec --- /dev/null +++ b/init.php @@ -0,0 +1,27 @@ +add_hook($host::HOOK_HOTKEY_MAP, $this); + } + + function hook_hotkey_map($hotkeys) { + $hotkeys["a N"] = "toggle_night_mode"; + return $hotkeys; + } + + function get_js() { + return file_get_contents(__DIR__ . "/init.js"); + } + + function api_version() { + return 2; + } + +}