setTimeout(() => { const searchInput = document.getElementById("faqSearch"); if (!searchInput) return; // Stop if not found searchInput.addEventListener("input", function () { let query = this.value.trim().toLowerCase().normalize("NFC"); // Normalize the query let accordionsHeaders = document.querySelectorAll(".accordions-head"); let accordionsContents = document.querySelectorAll(".accordion-content"); accordionsHeaders.forEach((accordion) => { let text = accordion.textContent.toLowerCase().normalize("NFC"); // Normalize the text if (text.includes(query)) { accordion.style.display = "block"; } highlightText(accordion, query); }); accordionsContents.forEach((accordion) => { let text = accordion.textContent.toLowerCase().normalize("NFC"); // Normalize the text if (text.includes(query)) { accordion.style.display = "block"; } highlightText(accordion, query); expandAccordion(accordion) }); }); }, 1000); // 1s delay to wait for elements to load function highlightText(element, query) { if (!element.getAttribute("data-original-text")) { element.setAttribute("data-original-text", element.innerHTML); } let originalText = element.getAttribute("data-original-text"); // Remove any existing highlights originalText = originalText.replace(/(.*?)<\/mark>/gi, "$1"); if (!query) { element.innerHTML = originalText; return; } const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const regex = new RegExp(escapedQuery, "gi"); // Process only the text nodes function wrapMatches(node) { if (node.nodeType === 3) { // TEXT_NODE const parent = node.parentNode; const frag = document.createDocumentFragment(); let lastIndex = 0; node.nodeValue.replace(regex, (match, offset) => { frag.appendChild(document.createTextNode(node.nodeValue.slice(lastIndex, offset))); let mark = document.createElement("mark"); mark.style.background = "yellow"; mark.style.color = "inherit"; mark.textContent = match; frag.appendChild(mark); lastIndex = offset + match.length; }); frag.appendChild(document.createTextNode(node.nodeValue.slice(lastIndex))); parent.replaceChild(frag, node); } else { node.childNodes.forEach(wrapMatches); } } // Start processing text nodes element.innerHTML = originalText; wrapMatches(element); } function expandAccordion(accordion) { let button = accordion.querySelector(".accordion-button"); if (button && !button.classList.contains("active")) { button.click(); // Simulate click to open } }