javascript - Iterate through HTML DOM and get depth - Stack Overflow

PHOTO EMBED

Tue May 03 2022 15:17:05 GMT+0000 (UTC)

Saved by @luckymage #node #depth

var tracker = {};
var depth = 0;
var prevNode;
Array.from(document.querySelectorAll("*")).forEach(node => {
    if (!tracker[node.tagName]) tracker[node.tagName] = 1;
    else tracker[node.tagName]++;
    console.log("Node depth:", node.tagName, depth);
    if (node.parentNode != prevNode) depth++;
    prevNode = node;
});
console.log(tracker);
content_copyCOPY

https://stackoverflow.com/questions/33903929/iterate-through-html-dom-and-get-depth