From 3b4e6f0b781d8f7812c8e98231bcf278ca0b6c5a Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 27 Aug 2023 23:20:43 +0300 Subject: [PATCH] Add debug functions menu --- public/index.html | 10 +++++++-- public/script.js | 2 ++ public/scripts/i18n.js | 9 +++++--- public/scripts/power-user.js | 35 ++++++++++++++++++++++++++++- public/scripts/templates/debug.html | 25 +++++++++++++++++++++ public/scripts/tokenizers.js | 6 ++--- public/style.css | 4 ++-- 7 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 public/scripts/templates/debug.html diff --git a/public/index.html b/public/index.html index 1c489eaea..de3dd0542 100644 --- a/public/index.html +++ b/public/index.html @@ -2961,8 +2961,14 @@ -
diff --git a/public/script.js b/public/script.js index 66a8f0778..bb3c90c03 100644 --- a/public/script.js +++ b/public/script.js @@ -70,6 +70,7 @@ import { MAX_CONTEXT_DEFAULT, renderStoryString, sortEntitiesList, + registerDebugFunction, } from "./scripts/power-user.js"; import { @@ -6658,6 +6659,7 @@ window["SillyTavern"].getContext = function () { saveReply, registerSlashCommand: registerSlashCommand, registerHelper: registerExtensionHelper, + registedDebugFunction: registerDebugFunction, }; }; diff --git a/public/scripts/i18n.js b/public/scripts/i18n.js index 5e359fa97..afc3f3917 100644 --- a/public/scripts/i18n.js +++ b/public/scripts/i18n.js @@ -1,3 +1,4 @@ +import { registerDebugFunction } from "./power-user.js"; import { waitUntilCondition } from "./utils.js"; const storageKey = "language"; @@ -48,9 +49,9 @@ function getMissingTranslations() { console.table(uniqueMissingData); console.log(missingDataMap); -} -window["getMissingTranslations"] = getMissingTranslations; + toastr.success(`Found ${uniqueMissingData.length} missing translations. See browser console for details.`); +} export function applyLocale(root = document) { const overrideLanguage = localStorage.getItem("language"); @@ -106,7 +107,6 @@ function addLanguagesToDropdown() { jQuery(async () => { waitUntilCondition(() => !!localeData); - window["applyLocale"] = applyLocale; applyLocale(); addLanguagesToDropdown(); @@ -121,4 +121,7 @@ jQuery(async () => { location.reload(); }); + + registerDebugFunction('getMissingTranslations', 'Get missing translations', 'Detects missing localization data and dumps the data into the browser console.', getMissingTranslations); + registerDebugFunction('applyLocale', 'Apply locale', 'Reapplies the currently selected locale to the page.', applyLocale); }); diff --git a/public/scripts/power-user.js b/public/scripts/power-user.js index 0a3c768a3..312db1edc 100644 --- a/public/scripts/power-user.js +++ b/public/scripts/power-user.js @@ -13,7 +13,8 @@ import { getCurrentChatId, printCharacters, setCharacterId, - setEditedMessageId + setEditedMessageId, + renderTemplate, } from "../script.js"; import { isMobile, initMovingUI } from "./RossAscends-mods.js"; import { @@ -232,6 +233,7 @@ const storage_keys = { }; let browser_has_focus = true; +const debug_functions = []; function playMessageSound() { if (!power_user.play_message_sound) { @@ -653,6 +655,22 @@ async function applyMovingUIPreset(name) { loadMovingUIState() } +/** + * Register a function to be executed when the debug menu is opened. + * @param {string} functionId Unique ID for the function. + * @param {string} name Name of the function. + * @param {string} description Description of the function. + * @param {function} func Function to be executed. + */ +export function registerDebugFunction(functionId, name, description, func) { + debug_functions.push({ functionId, name, description, func }); +} + +function showDebugMenu() { + const template = renderTemplate('debug', { functions: debug_functions }); + callPopup(template, 'text', '', { wide: true, large: true }); +} + switchUiMode(); applyFontScale('forced'); applyThemeColor(); @@ -2113,6 +2131,21 @@ $(document).ready(() => { saveSettingsDebounced(); }); + $('#debug_menu').on('click', function () { + showDebugMenu(); + }); + + $(document).on('click', '#debug_table [data-debug-function]', function () { + const functionId = $(this).data('debug-function'); + const functionRecord = debug_functions.find(f => f.functionId === functionId); + + if (functionRecord) { + functionRecord.func(); + } else { + console.warn(`Debug function ${functionId} not found`); + } + }); + $(window).on('focus', function () { browser_has_focus = true; }); diff --git a/public/scripts/templates/debug.html b/public/scripts/templates/debug.html new file mode 100644 index 000000000..3e562cc45 --- /dev/null +++ b/public/scripts/templates/debug.html @@ -0,0 +1,25 @@ +
+
+ {{this.name}}
+
+
+ {{this.description}}
+
+
+
+
+ |
+