1let loaded = false; 2let loadingPromise = null; 3 4function loadChatbotCSS() { 5 if (document.querySelector('link[data-chatbot-css]')) return; 6 7 const link = document.createElement('link'); 8 link.rel = 'stylesheet'; 9 link.href = `${window.hlx.codeBasePath}/blocks/chatbot/chatbot.css`; 10 link.dataset.chatbotCss = 'true'; 11 12 document.head.appendChild(link); 13} 14 15export default function loadWebchat() { 16 // If already loaded, resolve immediately 17 if (loaded || typeof window.initWebchat === 'function') { 18 return Promise.resolve(); 19 } 20 21 // If loading is in progress, return the existing promise to prevent race condition 22 if (loadingPromise) { 23 return loadingPromise; 24 } 25 26 loadingPromise = new Promise((resolve, reject) => { 27 // Load CSS ONLY when webchat is actually needed 28 loadChatbotCSS(); 29 30 const script = document.createElement('script'); 31 script.src = `${window.hlx.codeBasePath}/scripts/vendor/webchat.min.js`; 32 script.async = true; 33 34 script.onload = () => { 35 if (typeof window.initWebchat === 'function') { 36 loaded = true; 37 loadingPromise = null; 38 resolve(); 39 } else { 40 reject(new Error('webchat.min.js loaded but initWebchat missing')); 41 } 42 }; 43 44 script.onerror = () => { 45 loadingPromise = null; 46 reject(new Error('Failed to load webchat.min.js')); 47 }; 48 49 document.head.appendChild(script); 50 }); 51 52 return loadingPromise; 53}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.