// chatbot-controller.js function adjustIframeSize(height, width) { const iframe = document.getElementById('chatbot-iframe'); if (iframe) { const pageWidth = document.documentElement.clientWidth; const pageHeight = document.documentElement.clientHeight; let adjustedWidth = width; let adjustedHeight = height; if (width === '100%') { adjustedWidth = pageWidth; } else if (width.endsWith('px')) { const widthInPixels = parseInt(width, 10); adjustedWidth = Math.min(widthInPixels, pageWidth); } if (height === '100%') { adjustedHeight = pageHeight; } else if (height.endsWith('px')) { const heightInPixels = parseInt(height, 10); adjustedHeight = Math.min(heightInPixels, pageHeight); } iframe.style.width = `${adjustedWidth}px`; iframe.style.height = `${adjustedHeight}px`; } } window.addEventListener('message', (event) => { if (event.data.action === 'adjustSize') { const currentHeight = document.getElementById('chatbot-iframe').style.height; const currentWidth = document.getElementById('chatbot-iframe').style.width; // Only resize the chatbot when requested by the user if (currentHeight === '100vh' && currentWidth === '100%') { if (!('cta' in event.data)) { adjustIframeSize(event.data.height, event.data.width); } } else { adjustIframeSize(event.data.height, event.data.width); } } }); window.addEventListener('message', (event) => { if (event.data === "requestParentUrl") { event.source.postMessage({ action: 'responseParentUrl', url: window.location.href }, event.origin); } // Handle auto-login success callback if (event.data && event.data.action === 'autoLoginSuccess') { if (typeof window.onChatbotLoginSuccess === 'function') { window.onChatbotLoginSuccess(event.data.user); } } // Handle auto-login failure callback if (event.data && event.data.action === 'autoLoginFailed') { console.warn('Chatbot: Auto-login failed', event.data.error); if (typeof window.onChatbotLoginFailed === 'function') { window.onChatbotLoginFailed(event.data.error); } } // Handle logout success callback if (event.data && event.data.action === 'logoutSuccess') { if (typeof window.onChatbotLogout === 'function') { window.onChatbotLogout(); } } }); /** * Login with a signed JWT token * @param {string} token - The signed JWT token * @param {string} source - Optional source identifier */ function chatbotLoginWithToken(token, source) { const iframe = document.getElementById('chatbot-iframe'); if (iframe && iframe.contentWindow) { iframe.contentWindow.postMessage({ action: 'loginWithToken', token: token, source: source || 'external' }, '*'); } } /** * Logout from the chatbot */ function chatbotLogout() { const iframe = document.getElementById('chatbot-iframe'); if (iframe && iframe.contentWindow) { iframe.contentWindow.postMessage({ action: 'logout' }, '*'); } }