Skip to content

Instantly share code, notes, and snippets.

@benjameep
Created December 7, 2022 20:56
Show Gist options
  • Save benjameep/35a67886e9bd1989ab15e2baf46e9135 to your computer and use it in GitHub Desktop.
Save benjameep/35a67886e9bd1989ab15e2baf46e9135 to your computer and use it in GitHub Desktop.
Html Analysis
function allVisibleElements(root=document.body){
const iter = document.createNodeIterator(root, NodeFilter.SHOW_ELEMENT, {
acceptNode(e){
if(e.offsetWidth && e.offsetHeight && e.getClientRects().length)
return NodeFilter.FILTER_ACCEPT
}
})
const nodes = []
let node;
while(node = iter.nextNode())
nodes.push(node)
return nodes
}
var allVisibleElementProps = allVisibleElements().map(elm => {
var rect = elm.getBoundingClientRect(elm)
var props = {
type: elm.nodeName.toLowerCase(),
top: rect.top,
bottom: rect.bottom,
left: rect.left,
right: rect.right,
}
for(const attr of elm.attributes){
switch(attr.name){
case 'id':
props.id = elm.id
break
case 'class':
for(const name of elm.classList)
props['.'+name] = true
break
case 'style':
for(const style of elm.style)
props['style.'+style] = elm.style[style]
break
default:
props['@'+attr.name] = attr.value;
}
}
return props
})
console.log(JSON.stringify(allVisibleElementProps))
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment