From 34966da1dd3a2135f1e29ac6de635ad508e42129 Mon Sep 17 00:00:00 2001 From: Wx-2025 <351320169@qq.com> Date: Wed, 3 Sep 2025 12:31:49 +0800 Subject: [PATCH] Create cwb_index.js --- CharacterWorldBook/cwb_index.js | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 CharacterWorldBook/cwb_index.js diff --git a/CharacterWorldBook/cwb_index.js b/CharacterWorldBook/cwb_index.js new file mode 100644 index 0000000..906d837 --- /dev/null +++ b/CharacterWorldBook/cwb_index.js @@ -0,0 +1,49 @@ +import { loadSettings, bindSettingsEvents } from './src/cwb_settingsManager.js'; +import { initializeCharCardViewer } from './src/cwb_uiManager.js'; +import { initializeCore, getLatestChatName, resetScriptStateForNewChat, handleMessageReceived } from './src/cwb_core.js'; +import { checkForUpdates } from './src/cwb_updater.js'; +import { isCwbEnabled } from './src/cwb_utils.js'; +import { eventSource, event_types } from '/script.js'; + +const { jQuery } = window; + +export async function initializeCharacterWorldBook($cwbSettingsPanel) { + try { + if (!$cwbSettingsPanel || !$cwbSettingsPanel.length) { + console.error('[CWB] Invalid settings panel provided for initialization.'); + return; + } + + bindSettingsEvents($cwbSettingsPanel); + loadSettings(); + + if (!isCwbEnabled()) { + console.log('[CWB] Master switch is disabled. Halting core feature initialization.'); + initializeCharCardViewer(); + return; + } + + console.log('[CWB] Master switch is enabled. Initializing core features.'); + + initializeCharCardViewer(); + checkForUpdates(false, $cwbSettingsPanel); + await initializeCore($cwbSettingsPanel); + + eventSource.on(event_types.CHAT_CHANGED, async () => { + console.log('[CWB] Detected chat change. Resetting state.'); + setTimeout(async () => { + const newChatName = await getLatestChatName(); + await resetScriptStateForNewChat($cwbSettingsPanel, newChatName); + }, 150); + }); + + eventSource.on(event_types.MESSAGE_RECEIVED, () => { + handleMessageReceived($cwbSettingsPanel); + }); + + console.log('[CWB] Character World Book feature initialized successfully.'); + + } catch (error) { + console.error('[CWB] A critical error occurred during initialization:', error); + } +}