From a15446d3b1533064cdb25dc19939d9b5308cc5fc Mon Sep 17 00:00:00 2001 From: Ilia Ross Date: Fri, 19 Jul 2024 13:40:14 +0300 Subject: [PATCH] Fix to simplify the code and use no globals [build] --- ui-lib.pl | 43 ++++++++++++++++++++++++++++++--- unauthenticated/switch_theme.js | 33 ------------------------- 2 files changed, 39 insertions(+), 37 deletions(-) delete mode 100644 unauthenticated/switch_theme.js diff --git a/ui-lib.pl b/ui-lib.pl index a8946baee..e72f26e9c 100755 --- a/ui-lib.pl +++ b/ui-lib.pl @@ -2349,10 +2349,45 @@ Hotkeys are: sub ui_switch_theme_javascript { return &theme_ui_switch_theme_javascript(@_) if (defined(&theme_ui_switch_theme_javascript)); -my $switch_script = "\n"; -my $webmin_version = &get_webmin_version(); -$webmin_version =~ s/\.//g; -$switch_script .= "\n"; +my $webprefix = &get_webprefix(); +my $switch_script .= < +(function () { + let firstCombinationPressed = false; + document.addEventListener("keydown", function (event) { + // Check for Ctrl+Alt+T or Control+Option+T + if (event.ctrlKey && event.altKey && event.keyCode === 84) { + firstCombinationPressed = true; + + // Set a timeout to reset the state after a short period (e.g., 1 seconds) + setTimeout(() => { + firstCombinationPressed = false; + }, 1000); + } + if (firstCombinationPressed && event.shiftKey && + (event.keyCode === 65 || + event.keyCode === 70 || event.keyCode === 71 || + event.keyCode === 76)) { + const theme = + // Shift + A : Authentic theme + event.keyCode === 65 ? 1 : + // Shift + F / Shift + G : Framed theme / Gray theme + (event.keyCode === 70 || event.keyCode === 71) ? 2 : + // Shift + L : Legacy theme + event.keyCode === 76 ? 3 : null; + firstCombinationPressed = false; + try { + top.document.documentElement.style.filter = 'grayscale(100%) blur(0.5px) brightness(0.75) opacity(0.5)'; + top.document.documentElement.style.cursor = 'wait'; + top.document.documentElement.style.pointerEvents = 'none'; + } catch (error) {} + top.location.href = "$webprefix/switch_theme.cgi?theme=" + theme + ""; + } + }); +})(); +document.currentScript.remove(); + +EOF return $switch_script; } diff --git a/unauthenticated/switch_theme.js b/unauthenticated/switch_theme.js deleted file mode 100644 index ab313c197..000000000 --- a/unauthenticated/switch_theme.js +++ /dev/null @@ -1,33 +0,0 @@ -(function () { - let firstCombinationPressed = false; - document.addEventListener("keydown", function (event) { - // Check for Ctrl+Alt+T or Control+Option+T - if (event.ctrlKey && event.altKey && event.keyCode === 84) { - firstCombinationPressed = true; - - // Set a timeout to reset the state after a short period (e.g., 1 seconds) - setTimeout(() => { - firstCombinationPressed = false; - }, 1000); - } - if (firstCombinationPressed && event.shiftKey && - (event.keyCode === 65 || - event.keyCode === 70 || event.keyCode === 71 || - event.keyCode === 76)) { - const theme = - // Shift + A : Authentic theme - event.keyCode === 65 ? 1 : - // Shift + F / Shift + G : Framed theme / Gray theme - (event.keyCode === 70 || event.keyCode === 71) ? 2 : - // Shift + L : Legacy theme - event.keyCode === 76 ? 3 : null; - firstCombinationPressed = false; - try { - top.document.documentElement.style.filter = 'grayscale(100%) blur(0.5px) brightness(0.75) opacity(0.5)'; - top.document.documentElement.style.cursor = 'wait'; - top.document.documentElement.style.pointerEvents = 'none'; - } catch (error) {} - top.location.href = __webmin_webprefix__ + "/switch_theme.cgi?theme=" + theme + ""; - } - }); -})();