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),
});
}
It does work (ignores
<script>/<style>
) because withNodeFilter.FILTER_REJECT
those nodes are ignored completely, and we never reach the text nodes inside:https://developer.mozilla.org/en-US/docs/Web/API/NodeFilter/acceptNode