(function () { var dispatchEvent = function (eventName, details) { document.dispatchEvent( new CustomEvent(eventName, { detail: details, }) ); } // Add an event listener var onChatbot = function (eventName, handler) { document.addEventListener(eventName, handler); } var closeChatbot = function () { const chatbot = document.getElementById("LMChatbotContainer"); chatbot.style = "display: none"; window.lmChatbotStatus = "Closed"; document.dispatchEvent(new CustomEvent("LMChatbotClosed")); document.body.style.removeProperty("overflow"); // Return focus to the element that triggered the chatbot if (window.lmChatbotTriggerElement) { window.lmChatbotTriggerElement.focus(); } } var wait = function (milliseconds, until) { return new Promise(function (resolve) { setTimeout(function () { if (!until) { resolve(); } else if (until()) { resolve() } else { resolve(wait(milliseconds, until)) } }, milliseconds); }); } var getCookie = function (cookieName) { return document.cookie .split("; ") .find((row) => row.startsWith(cookieName)); } var checkCookie = function (cookieName, cookieValue) { return new Promise(function (resolve, reject) { var cookie = getCookie(cookieName); if (!cookie && window.CassieWidgetLoader && window.CassieWidgetLoader.Widget && !window.CassieWidgetLoader.Widget.hasConsent()) { wait(5000, function () { return getCookie(cookieName); }) .then(function () { cookie = getCookie(cookieName); if (cookie === (cookieName + '=' + cookieValue)) { resolve(); } else { reject(); } }).catch(function (err) { reject(err); }) } else { if (cookie === (cookieName + '=' + cookieValue)) { resolve(); } else { reject(); } } }) } var checkABVersions = function (jwt, done, url) { try { if (window.adobe && window.adobe.target) { adobe.target.getOffer({ "mbox": "lm-chatbot-ab-testing", "success": function (offer) { // rewrite for null check const chatbotVersion = offer && offer[0] && offer[0].content && offer[0].content[0] ? offer[0].content[0].chatbotVersion : null; url = chatbotVersion ? `${url}?botType=${chatbotVersion}` : url; callConfigAPI(jwt, done, url); }, "error": function (error) { console.log(error); callConfigAPI(jwt, done, url); } }); } else if (window.alloy) { alloy("sendEvent", { decisionScopes: ["lm-chatbot-ab-testing"] }).then(function (data) { const scope = data.decisions.find(function (value) { return value.scope === "lm-chatbot-ab-testing" }); const chatbotVersion = scope ? scope.items[0].data.content.chatbotVersion : null; url = chatbotVersion ? `${url}?botType=${chatbotVersion}` : url; callConfigAPI(jwt, done, url); }).catch(function (err) { callConfigAPI(jwt, done, url); }) } } catch (err) { console.error(err); callConfigAPI(jwt, done, url); } } var getConfig = function (jwt, done) { let url = "https://api.assist.lilly.com/api/config"; if (jwt) { window.sessionStorage.setItem("LMChatbotJWT", jwt); } else { jwt = window.sessionStorage.getItem("LMChatbotJWT"); } if ((window.adobe && window.adobe.target) || window.alloy) { checkCookie('consent_adv_and_mark', 'true').then(function () { checkABVersions(jwt, done, url); }).catch(function (err) { callConfigAPI(jwt, done, url); }) } else { callConfigAPI(jwt, done, url); } }; function callConfigAPI(jwt, done, url) { let request = new XMLHttpRequest(); request.onreadystatechange = function () { if (this.readyState === 4 && this.status === 200) { const response = JSON.parse(this.responseText); done(null, response); } else if (this.readyState === 4 && this.status === 401) { dispatchEvent("LMChatbotError", { type: "InitiationFailed", error: this.responseText }); done(this.responseText); console.error("Couldn't get the chatbot configuration."); } else if (this.readyState === 4 && this.status === 500) { dispatchEvent("LMChatbotError", { type: "InitiationFailed", error: this.responseText }); done(this.responseText); console.error("Couldn't get the chatbot configuration."); } }; request.open("GET", url, true); request.setRequestHeader("Authorization", jwt); request.send(); } var files = [ { type: "css", url: "https://assets.assist.lilly.com/public/widget/static/css/main.7be94848.css", }, { type: "js", url: "https://assets.assist.lilly.com/public/widget/static/js/main.30cead82.js", }, ]; var initChatbot = function (jwt) { if (!document.getElementById("LMChatbotContainer")) { var rootElement = document.createElement("div"); rootElement.id = "LMChatbotContainer"; rootElement.style = "display: block"; document.body.appendChild(rootElement); const titles = document.getElementsByTagName("title"); window.lmChatbotWebsiteHeader = titles[0].innerHTML; window.lmChatbotTriggerElement = document.activeElement; } files.forEach(function (file) { if (file.type === "js") { var scriptTag = document.createElement("script"); scriptTag.type = "text/javascript"; scriptTag.src = file.url; scriptTag.defer = "defer"; document.body.appendChild(scriptTag); scriptTag.onload = function () { getConfig(jwt, function (err, config) { console.log(err); if (err) { console.log("Chatbot Couldn't be initialized :("); } window.sessionStorage.setItem("LMChatbotConfig", JSON.stringify(config)); dispatchEvent("LMChatbotInitiated", config); }); }; } else if (file.type === "css") { var linkTag = document.createElement("link"); linkTag.type = "text/css"; linkTag.rel = "stylesheet"; linkTag.href = file.url; document.body.appendChild(linkTag); } }); }; let originalTabIndexes = []; function storeAndDisableTabIndexes() { const allFocusableElements = document.querySelectorAll('a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])'); allFocusableElements.forEach((element, index) => { if (!element.closest('.cb-App-chatwidget')) { originalTabIndexes.push({ element: element, tabindex: element.getAttribute('tabindex') }); element.setAttribute('tabindex', '-1'); } }); } function restoreTabIndexes() { originalTabIndexes.forEach(item => { item.element.removeAttribute('tabindex'); }); originalTabIndexes = []; } let intervalId; // Continuously check for the presence of .cb-App-chatwidget and LMChatbotContainer display status function CheckIfWidgetOpen() { intervalId = setInterval(function () { const chatWidget = document.querySelector(".cb-App-chatwidget"); const chatbotContainer = document.getElementById("LMChatbotContainer"); if (chatWidget && chatbotContainer && chatbotContainer.style.display === "block") { storeAndDisableTabIndexes(); clearInterval(intervalId); // Stop the interval once the chatbot is opened } }, 1000); } CheckIfWidgetOpen(); // Listen for chatbot close event to restore tab indexes and restart the interval document.addEventListener("LMChatbotClosed", function () { restoreTabIndexes(); CheckIfWidgetOpen(); // Restart the interval once the chatbot is closed }); window.lmChatbot = { init: initChatbot, getConfig: getConfig, on: onChatbot, close: closeChatbot }; })();