Yes, it is definitely achievable, but it is crucial to provide the parent element. It's technically feasible to navigate through the entire DOM and each node, however, it's advisable to avoid doing so for efficiency.
elements = document.body.childNodes;
elementsToRemove = [];
function mergeTextNodes(element, previousText) {
if (element.nextSibling && element.nextSibling.nodeType == 3) {
elementsToRemove.push(element.nextSibling);
return mergeTextNodes(element.nextSibling, previousText + element.nodeValue);
} else {
return previousText + element.nodeValue;
}
}
for (j = 0; j < elements.length; j++) {
if (elements[j].nodeType == 3) {
elements[j].nodeValue = mergeTextNodes(elements[j], '');
}
}
for (j = 0; j < elementsToRemove.length; j++) {
console.log(elementsToRemove[j]);
elementsToRemove[j].remove();
}