// This script manages the behavior of a chatbot iframe on a web page. // It handles resizing, minimizing/maximizing, and mobile-specific behavior. // Get references to the chatbot container and iframe elements. var iframe = document.getElementById('chatbot-iframe'); var container = document.getElementById('chatbot-container'); // Check for previous session state stored in localStorage. var minimizedAt = parseInt(localStorage.getItem('chatbotMinimizedAt'), 10); var isMinimized = localStorage.getItem('chatbotMinimized') === 'true'; var now = Date.now(); var expired = isNaN(minimizedAt) || (now - minimizedAt > 3600000); // Check if session is older than 1 hour. // Initialize the session state if it's expired or doesn't exist. if (expired || localStorage.getItem('chatbotMinimized') === null) { var isMobile = window.innerWidth <= 600; localStorage.setItem('chatbotMinimized', isMobile ? 'true' : 'false'); localStorage.setItem('chatbotMinimizedAt', now.toString()); localStorage.setItem('chatbotSessionInitialized', 'true'); isMinimized = isMobile; } // Function to minimize the chatbot container. function minimizeContainer() { iframe.style.width = '80px'; iframe.style.height = '80px'; container.style.width = '80px'; container.style.height = '80px'; container.style.bottom = '10px'; container.style.overflow = 'hidden'; } // Function to maximize the chatbot container. function maximizeContainer() { iframe.style.width = '340px'; iframe.style.height = '520px'; container.style.width = '340px'; container.style.height = '520px'; container.style.bottom = '10px'; container.style.overflow = 'visible'; } // Listen for messages from the iframe to control its state. window.addEventListener('message', function(event) { // Check if the message is from a trusted origin (not strictly necessary but good practice). // For now, we'll listen to all messages with a valid action. if (event.data.action === 'iframe-ready') { // Show the container once the iframe is ready to receive messages. container.style.display = 'block'; if (isMinimized) { minimizeContainer(); iframe.contentWindow.postMessage({ action: 'minimize' }, '*'); } else { maximizeContainer(); iframe.contentWindow.postMessage({ action: 'maximize' }, '*'); } } else if (event.data.action === 'minimize') { // Handle a minimize request from the iframe. minimizeContainer(); localStorage.setItem('chatbotMinimized', 'true'); localStorage.setItem('chatbotMinimizedAt', Date.now().toString()); } else if (event.data.action === 'maximize') { // Handle a maximize request from the iframe. maximizeContainer(); localStorage.setItem('chatbotMinimized', 'false'); localStorage.setItem('chatbotMinimizedAt', Date.now().toString()); } }); // Send a mobile-mode message to the iframe on load if the window is small. iframe.onload = function () { if (window.innerWidth <= 600) { iframe.contentWindow.postMessage({ action: 'mobile-mode' }, '*'); } };