Skip to content

Instantly share code, notes, and snippets.

@andy-blum
Created April 22, 2020 17:34
Show Gist options
  • Save andy-blum/2d94016e8a0f08b3124bc68f96a35322 to your computer and use it in GitHub Desktop.
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
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