diff --git a/public/script.js b/public/script.js index a160d723b..4665ab038 100644 --- a/public/script.js +++ b/public/script.js @@ -5543,21 +5543,20 @@ function openCharacterWorldPopup() { const fileName = getCharaFilename(chid); const tempExtraBooks = selectedWorlds.map((index) => world_names[index]).filter((e) => e !== undefined); - const existingCharLore = charLore.find((e) => e.name === fileName); - if (existingCharLore) { - if (tempExtraBooks.length === 0) { - charLore.splice(existingCharLore, 1); - } else { - existingCharLore.extraBooks = tempExtraBooks; - } - } else { + const existingCharIndex = charLore.findIndex((e) => e.name === fileName); + if (existingCharIndex === -1) { const newCharLoreEntry = { name: fileName, extraBooks: tempExtraBooks } charLore.push(newCharLoreEntry); + } else if (tempExtraBooks.length === 0) { + charLore.splice(existingCharIndex, 1); + } else { + charLore[existingCharIndex].extraBooks = tempExtraBooks; } + Object.assign(world_info, { charLore: charLore }); saveSettingsDebounced(); } diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index 81df9197f..c292127e8 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -250,16 +250,18 @@ function displayWorldEntries(name, data) { return; } - const existingCharLores = world_info.charLore?.filter((e) => e.extraBooks.includes(name)); - if (existingCharLores && existingCharLores.length > 0) { - existingCharLores.forEach((charLore) => { - const tempCharLore = charLore.extraBooks.filter((e) => e !== name); - if (tempCharLore.length === 0) { - world_info.charLore.splice(charLore, 1); - } else { - charLore.extraBooks = tempCharLore; + if (world_info.charLore) { + world_info.charLore.forEach((charLore, index) => { + if (charLore.extraBooks?.includes(name)) { + const tempCharLore = charLore.extraBooks.filter((e) => e !== name); + if (tempCharLore.length === 0) { + world_info.charLore.splice(index, 1); + } else { + charLore.extraBooks = tempCharLore; + } } }); + saveSettingsDebounced(); }