From 4bd29a207a813e50fa557e1067f433d0eed738ae Mon Sep 17 00:00:00 2001 From: ARIA Date: Sun, 12 Jul 2026 17:33:08 +0200 Subject: [PATCH] Fixes #20: Fix JSON parse errors and empty equipment tab - Replace JSON.parse() with repairJSON() in desktop.js and mobile.js to handle malformed JSON from infoBox/userStats data - Add defensive initialization in renderEquipment(): ensure showEquipment defaults to true and equipment data structure exists - Add explicit renderEquipment() call during initialization --- index.js | 2 ++ src/systems/rendering/equipment.js | 19 +++++++++++++++++++ src/systems/ui/desktop.js | 5 +++-- src/systems/ui/mobile.js | 7 ++++--- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 1db8294..c298000 100644 --- a/index.js +++ b/index.js @@ -349,6 +349,8 @@ jQuery(async () => { initThoughtBasedExpressions(); updateFabWidgets(); updateStripWidgets(); + // Explicit initial render of equipment to ensure it's visible + renderEquipment(); } catch (error) { console.error('[RPG Companion] Chat data load failed, using defaults:', error); } diff --git a/src/systems/rendering/equipment.js b/src/systems/rendering/equipment.js index 4bd0c6c..efb934c 100644 --- a/src/systems/rendering/equipment.js +++ b/src/systems/rendering/equipment.js @@ -146,10 +146,29 @@ function generateEquipmentHTML() { * Gets data from state/settings and updates DOM directly. */ export function renderEquipment() { + // Ensure showEquipment defaults to true if undefined + if (extensionSettings.showEquipment === undefined) { + extensionSettings.showEquipment = true; + } + if (!$equipmentContainer || !extensionSettings.showEquipment) { return; } + // Ensure equipment data structure exists (defensive initialization) + if (!extensionSettings.userStats?.equipment) { + extensionSettings.userStats = extensionSettings.userStats || {}; + extensionSettings.userStats.equipment = { + items: [], + slots: { + helmet: null, ring1: null, ring2: null, ring3: null, ring4: null, + ring5: null, ring6: null, ring7: null, ring8: null, ring9: null, ring10: null, + necklace: null, bodyArmor: null, pants: null, shoes: null, gloves: null, + accessory1: null, accessory2: null, accessory3: null + } + }; + } + const html = generateEquipmentHTML(); updateIfChanged($equipmentContainer, html, 'rpg-equipment'); diff --git a/src/systems/ui/desktop.js b/src/systems/ui/desktop.js index edfa5f0..582cf48 100644 --- a/src/systems/ui/desktop.js +++ b/src/systems/ui/desktop.js @@ -6,6 +6,7 @@ import { i18n } from '../../core/i18n.js'; import { extensionSettings, lastGeneratedData, committedTrackerData } from '../../core/state.js'; import { hexToRgba } from './theme.js'; +import { repairJSON } from '../../utils/jsonRepair.js'; /** * Helper to parse time string and calculate clock hand angles @@ -50,7 +51,7 @@ export function updateStripWidgets() { let infoData = null; if (infoBox) { try { - infoData = typeof infoBox === 'string' ? JSON.parse(infoBox) : infoBox; + infoData = typeof infoBox === 'string' ? repairJSON(infoBox) : infoBox; } catch (e) { console.warn('[RPG Strip Widgets] Failed to parse infoBox:', e); } @@ -124,7 +125,7 @@ export function updateStripWidgets() { const userStatsData = lastGeneratedData?.userStats || committedTrackerData?.userStats; if (userStatsData) { try { - const parsedStats = typeof userStatsData === 'string' ? JSON.parse(userStatsData) : userStatsData; + const parsedStats = typeof userStatsData === 'string' ? repairJSON(userStatsData) : userStatsData; if (parsedStats?.stats) { allStats = parsedStats.stats; } diff --git a/src/systems/ui/mobile.js b/src/systems/ui/mobile.js index cf47c5e..60188c6 100644 --- a/src/systems/ui/mobile.js +++ b/src/systems/ui/mobile.js @@ -9,6 +9,7 @@ import { closeMobilePanelWithAnimation, updateCollapseToggleIcon } from './layou import { setupDesktopTabs, removeDesktopTabs } from './desktop.js'; import { i18n } from '../../core/i18n.js'; import { hexToRgba } from './theme.js'; +import { repairJSON } from '../../utils/jsonRepair.js'; /** * Updates the text labels of the mobile navigation tabs based on the current language. @@ -1322,7 +1323,7 @@ export function updateFabWidgets() { let infoData = null; if (infoBox) { try { - infoData = typeof infoBox === 'string' ? JSON.parse(infoBox) : infoBox; + infoData = typeof infoBox === 'string' ? repairJSON(infoBox) : infoBox; } catch (e) { console.warn('[RPG FAB Widgets] Failed to parse infoBox:', e); } @@ -1332,7 +1333,7 @@ export function updateFabWidgets() { let statsData = null; if (userStats) { try { - statsData = typeof userStats === 'string' ? JSON.parse(userStats) : userStats; + statsData = typeof userStats === 'string' ? repairJSON(userStats) : userStats; } catch (e) { console.warn('[RPG FAB Widgets] Failed to parse userStats:', e); } @@ -1444,7 +1445,7 @@ export function updateFabWidgets() { let allStats = []; try { const userStatsJson = extensionSettings.userStats; - const parsedUserStats = typeof userStatsJson === 'string' ? JSON.parse(userStatsJson) : userStatsJson; + const parsedUserStats = typeof userStatsJson === 'string' ? repairJSON(userStatsJson) : userStatsJson; if (parsedUserStats?.stats) { allStats = parsedUserStats.stats; }