A general-purpose DOM tree walker based on https://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page (Phrogz' answer and its comments).
The textNodesUnder()
function would then look like this:
function textNodesUnder(el) {
return walkNodeTree(el, {
inspect: n => !['STYLE', 'SCRIPT'].includes(n.nodeName),
collect: n => (n.nodeType === Node.TEXT_NODE),
//callback: n => console.log(n.nodeName, n),
});
}
The
textNodesUnder
example function currently has a mistake: text insidestyle
/script
tags won't be filtered out because you should be checking the node's parent, not the text node itself.Edit: I'm wrong, see below.