diff --git a/index.js b/index.js index 68864a1..17c9e62 100644 --- a/index.js +++ b/index.js @@ -93,6 +93,46 @@ let $userStatsContainer = null; let $infoBoxContainer = null; let $thoughtsContainer = null; +/** + * Safely attempts to get a thumbnail URL with proper error handling. + * Returns null if the URL cannot be generated to avoid 400 Bad Request errors. + * + * @param {string} type - The type of thumbnail ('persona' or 'avatar') + * @param {string} filename - The filename to get thumbnail for + * @returns {string|null} - The thumbnail URL or null if it fails + */ +function getSafeThumbnailUrl(type, filename) { + // Return null if no filename provided + if (!filename || filename === 'none') { + console.log(`[RPG Companion] No valid filename provided for ${type} thumbnail`); + return null; + } + + try { + // Attempt to get thumbnail URL from SillyTavern API + const url = getThumbnailUrl(type, filename); + + // Validate that we got a string back + if (typeof url !== 'string' || url.trim() === '') { + console.warn(`[RPG Companion] getThumbnailUrl returned invalid result for ${type}:`, filename); + return null; + } + + console.log(`[RPG Companion] Successfully generated ${type} thumbnail URL for: ${filename}`); + return url; + } catch (error) { + // Log detailed error information for debugging + console.error(`[RPG Companion] Failed to get ${type} thumbnail for "${filename}":`, error); + console.error('[RPG Companion] Error details:', { + type, + filename, + errorMessage: error.message, + errorStack: error.stack + }); + return null; + } +} + /** * Loads the extension settings from the global settings object. */ @@ -2739,14 +2779,15 @@ function renderUserStats() { } // Get user portrait - handle both default-user and custom persona folders - let userPortrait = 'img/user-default.png'; // fallback + // Use a transparent placeholder as fallback to avoid 400 errors + const transparentPixel = 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="100" height="100"%3E%3Crect width="100" height="100" fill="%23cccccc" opacity="0.3"/%3E%3Ctext x="50%25" y="50%25" text-anchor="middle" dy=".3em" fill="%23666" font-size="40"%3E%3F%3C/text%3E%3C/svg%3E'; + let userPortrait = transparentPixel; + if (user_avatar) { - // Try to get the thumbnail, but have a fallback - try { - userPortrait = getThumbnailUrl('persona', user_avatar) || 'img/user-default.png'; - } catch (e) { - console.warn('[RPG Companion] Could not load user avatar, using default', e); - userPortrait = 'img/user-default.png'; + // Try to get the thumbnail using our safe helper + const thumbnailUrl = getSafeThumbnailUrl('persona', user_avatar); + if (thumbnailUrl) { + userPortrait = thumbnailUrl; } } @@ -2757,7 +2798,7 @@ function renderUserStats() {