// Create an iframe element const iframe = document.createElement('iframe'); iframe.id = "iframe"; var isOpen = false; const url = 'https://widget.chatbotly.co/widget/f99ae841-23c1-4dd3-b320-381686203b57'; // Set the src attribute of the iframe to the desired URL fetch(url) .then(response => { if (response.ok) { iframe.src = url; } else { console.error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`); } }) .catch(error => { console.error(`Failed to fetch ${url}: ${error}`); }); iframe.style.position = 'fixed'; iframe.style.bottom = '0'; iframe.style.border = 'none'; iframe.style.zIndex = '9999999999'; // Dynamically set the horizontal position based on widget_position iframe.style.right = '0px'; iframe.style.left = 'auto'; // Reset left to avoid conflicts if (window.matchMedia("(max-device-width: 480px)").matches) { iframe.style.width = '100px'; iframe.style.height = '100px'; } else { iframe.style.width = '310px'; iframe.style.height = '200px'; } // Set height of iframe dynamically based on whether search bar is visible function setIframeHeight() { // Only resize when the chatbot window is open and only for mobile version if (isOpen && window.matchMedia("(max-device-width: 480px)").matches) { // Check if search bar is visible if (typeof window.visualViewport !== 'undefined' && window.visualViewport.height < window.innerHeight) { // Calculate available height for iframe const searchBarHeight = window.innerHeight - window.visualViewport.height; const availableHeight = window.innerHeight - searchBarHeight; // Set height of iframe to available height iframe.style.height = availableHeight + 'px'; } else { // Set height of iframe to full screen height iframe.style.height = window.innerHeight + 'px'; } } } // Call setIframeHeight() initially and on window resize window.addEventListener('resize', setIframeHeight); // Wait for the iframe to load before sending messages iframe.onload = function() { const currentURL = window.location.href; iframe.contentWindow.postMessage(currentURL, "*"); }; document.body.appendChild(iframe); function toggleElement() { var iframe = document.getElementById("iframe"); isOpen = isOpen ? false : true; if (window.matchMedia("(max-device-width: 480px)").matches) { iframe.style.width = (iframe.style.width == "100px") ? "100vw" : "100px"; iframe.style.height = (iframe.style.height == "100px") ? "100vw": "100px"; setIframeHeight(); } else { const dynamicHeight = Math.min(850, window.innerHeight) + 'px'; iframe.style.width = (iframe.style.width == "310px") ? "550px" : "310px"; iframe.style.height = (iframe.style.height == "200px") ? dynamicHeight : "200px"; } } window.addEventListener("message", function(event) { if (event.data == "toggle") { toggleElement(); } });