From 654a34f932846da4396cdebcd08860b65ab2ab98 Mon Sep 17 00:00:00 2001 From: Randall Fitzgerald Date: Fri, 22 Sep 2023 14:13:58 -0400 Subject: [PATCH] Added custom CSS box to UI Theme settings (#1166) * Added custom CSS box to UI Theme settings * Update index.html Merged against release instead of staging. Whoops. * Added an import stripper regex so that imports will be removed and show a toastr to inform the user. * Moved import remove code to applyCustomCSS. Updated localStorage. Not re-running saveSettingsDebounced() --- public/index.html | 8 ++++++ public/scripts/power-user.js | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/public/index.html b/public/index.html index 792b09335..b508cc697 100644 --- a/public/index.html +++ b/public/index.html @@ -2940,6 +2940,14 @@ +
+

+ Custom CSS +

+
+ +
+
diff --git a/public/scripts/power-user.js b/public/scripts/power-user.js index abe69b382..4dd84b1c3 100644 --- a/public/scripts/power-user.js +++ b/public/scripts/power-user.js @@ -129,6 +129,8 @@ let power_user = { shadow_color: `${getComputedStyle(document.documentElement).getPropertyValue('--SmartThemeShadowColor').trim()}`, border_color: `${getComputedStyle(document.documentElement).getPropertyValue('--SmartThemeBorderColor').trim()}`, + custom_css: '', + waifuMode: false, movingUI: false, @@ -231,6 +233,8 @@ const storage_keys = { shadow_width: "TavernAI_shadow_width", border_color: "TavernAI_border_color", + custom_css: "TavernAI_custom_css", + waifuMode: "TavernAI_waifuMode", movingUI: "TavernAI_movingUI", noShadows: "TavernAI_noShadows", @@ -557,6 +561,33 @@ async function applyThemeColor(type) { } } +async function applyCustomCSS() +{ + power_user.custom_css = String(localStorage.getItem(storage_keys.custom_css) ?? ""); + + if (power_user.custom_css.includes("@import")) + { + var removeImport = /@import[^;]+;/gm + power_user.custom_css = power_user.custom_css.replace(removeImport, ""); + toastr.warning(power_user.custom_css); + $('#customCSS').val(power_user.custom_css); + localStorage.setItem(storage_keys.custom_css, power_user.custom_css); + toastr.warning('@import not allowed in Custom CSS. @import lines removed.') + } + + $("#customCSS").val(power_user.custom_css); + var styleId = "custom-style"; + var style = document.getElementById(styleId); + if (!style) + { + style = document.createElement("style"); + style.setAttribute("type", "text/css"); + style.setAttribute("id", styleId); + document.head.appendChild(style); + } + style.innerHTML = power_user.custom_css; +} + async function applyBlurStrength() { power_user.blur_strength = Number(localStorage.getItem(storage_keys.blur_strength) ?? 1); document.documentElement.style.setProperty('--blurStrength', power_user.blur_strength); @@ -615,6 +646,13 @@ async function applyTheme(name) { await applyBlurStrength(); } }, + { + key: 'custom_css', + action: async () => { + localStorage.setItem(storage_keys.custom_css, power_user.custom_css); + await applyCustomCSS(); + } + }, { key: 'shadow_width', action: async () => { @@ -781,6 +819,7 @@ applyChatWidth('forced'); applyAvatarStyle(); applyBlurStrength(); applyShadowWidth(); +applyCustomCSS(); switchMovingUI(); noShadows(); switchHotswap(); @@ -1284,6 +1323,7 @@ async function saveTheme() { expand_message_actions: power_user.expand_message_actions, hotswap_enabled: power_user.hotswap_enabled, + custom_css: power_user.custom_css, }; @@ -1893,6 +1933,14 @@ $(document).ready(() => { saveSettingsDebounced(); }); + $("#customCSS").on('change', () => + { + power_user.custom_css = $('#customCSS').val(); + localStorage.setItem(storage_keys.custom_css, power_user.custom_css); + saveSettingsDebounced(); + applyCustomCSS(); + }); + $("#movingUImode").change(function () { power_user.movingUI = $(this).prop("checked"); localStorage.setItem(storage_keys.movingUI, power_user.movingUI);