Created
April 22, 2020 17:34
-
-
Save andy-blum/2d94016e8a0f08b3124bc68f96a35322 to your computer and use it in GitHub Desktop.
Function to get array of child nodes that are visible in browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getVisibleChildNodes(input) { | |
if (!input instanceof Node) { | |
return false; | |
} else { | |
const filteredNodes = | |
// Create array from child nodes | |
Array.from(input.childNodes) | |
// Remove comments | |
.filter(function(node){ | |
if (node.nodeName != '#comment') {return node;} | |
}) | |
// Remove nodes that are only whitespace | |
.filter(function(node){ | |
if (node.nodeName != '#text'){ | |
return node; | |
} else if (node.wholeText.replace(/\s*/g,"") != "") { | |
return node; | |
} | |
}); | |
return filteredNodes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment