Thank you, I hope this is a top priority.
I have it mostly working, but your team should be able to write something more efficient.
// JavaScript Document
function toggleChatbot() {
var chatbotWindow = document.getElementById(“mwai-chatbot-roary-chat”);
if (!chatbotWindow) return;
var isOpen = chatbotWindow.classList.contains("mwai-open");
if (!isOpen) {
chatbotWindow.classList.add("mwai-open");
chatbotWindow.setAttribute("aria-hidden", "false");
// Wait for the chatbot to open, then move focus to the input
setTimeout(function () {
var chatbotInput = chatbotWindow.querySelector(".mwai-input textarea");
if (chatbotInput) {
chatbotInput.setAttribute("tabindex", "0");
chatbotInput.focus();
}
}, 300);
} else {
chatbotWindow.classList.remove("mwai-open");
chatbotWindow.setAttribute("aria-hidden", "true");
}
}
function attachEventListeners() {
var chatbotTrigger = document.querySelector(“.mwai-trigger”);
if (!chatbotTrigger) return;
chatbotTrigger.setAttribute("tabindex", "0");
chatbotTrigger.setAttribute("role", "button");
chatbotTrigger.setAttribute("aria-label", "Open AI Chatbot");
chatbotTrigger.addEventListener("keydown", function (event) {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault(); // Prevent scrolling on space key
toggleChatbot();
}
});
chatbotTrigger.addEventListener("click", function () {
toggleChatbot();
});
}
// Start checking for the chatbot trigger after the page has loaded
document.addEventListener(“DOMContentLoaded”, function () {
var checkInterval = setInterval(function () {
var chatbotTrigger = document.querySelector(“.mwai-trigger”);
if (chatbotTrigger) {
attachEventListeners();
clearInterval(checkInterval); // Stop checking once the trigger is found
}
}, 1000); // Check every second
});