function dom2json() { const jsContainer = document.querySelector("#jsContainer") function domJson(dom) { var obj = { tag: getTagName(dom) } if (dom.nodeType == 1) { var attrs = getTagAttrs(dom) if (attrs) obj.attributes = attrs; console.log(dom.children) obj.children = Array.from(dom.childNodes).filter(child => { return !(child.nodeType == 3 && !child.textContent.trim()) }).map(child => domJson(child)) return obj } if (dom.nodeType == 3) { obj.content = texthandle(dom.textContent) return obj } } function texthandle(str) { return str.replace(/\s/g, '') } function getTagName(dom) { return dom.nodeName.toLocaleLowerCase().replace('#', '') } function getTagAttrs(dom) { var attr = Array.from(dom.attributes) var obj = {} attr.forEach(atr => obj[atr.name] = atr.value) return attr.length ? obj : null; } return domJson(jsContainer) }