UNPKG

owtlab-tracking

Version:
32 lines (28 loc) 851 B
export function getDomNodePath(el) { if (!el.nodeName) return ''; const stack = []; while (el.parentNode != null) { // console.log(el.nodeName); let sibCount = 0; let sibIndex = 0; for (let i = 0; i < el.parentNode.childNodes.length; i++) { const sib = el.parentNode.childNodes[i]; if (sib.nodeName == el.nodeName) { if (sib === el) { sibIndex = sibCount; } sibCount++; } } if (el.hasAttribute('id') && el.id != '') { stack.unshift(`${el.nodeName.toLowerCase()}#${el.id}`); } else if (sibCount > 1) { stack.unshift(`${el.nodeName.toLowerCase()}:eq(${sibIndex})`); } else { stack.unshift(el.nodeName.toLowerCase()); } el = el.parentNode; } return stack.slice(1).join(' > '); } // via: http://stackoverflow.com/a/16742828/2511985