I found a fixed by changing the custom javascript to:
function replace(element, from, to) {
if (element.childNodes.length) {
element.childNodes.forEach(child => replace(child, from, to));
} else {
const cont = element.textContent;
if (cont) element.textContent = cont.replace(from, to);
}
};
replace(document.body, new RegExp("hello", "g"), "hi");
It all works now. Thanks.