Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › highlighter results › Reply To: highlighter results
March 27, 2025 at 10:25 am
#53501
Participant
Ah ok no worries, that explains it 🙂
I have written some script to get around it for now:
document.addEventListener("DOMContentLoaded", function () {
const params = new URLSearchParams(window.location.search);
const keyword = params.get('asp_ls');
if (!keyword) return;
const container = document.querySelector('#highlighter');
if (!container) return;
const regex = new RegExp(<code>(${keyword})</code>, 'gi');
// Recursive function to walk through text nodes
function highlightTextNodes(node) {
if (node.nodeType === Node.TEXT_NODE) {
if (regex.test(node.textContent)) {
const span = document.createElement('span');
span.innerHTML = node.textContent.replace(regex, '<mark>$1</mark>');
node.replaceWith(span);
}
} else if (node.nodeType === Node.ELEMENT_NODE && node.nodeName !== "SCRIPT" && node.nodeName !== "STYLE") {
for (let i = 0; i < node.childNodes.length; i++) {
highlightTextNodes(node.childNodes[i]);
}
}
}
highlightTextNodes(container);
});