getXpath = function(dom) {
dom = $(dom).get(0);
var path = "";
for (; dom && dom.nodeType == 1; dom = dom.parentNode) {
var index = 1;
for (var sib = dom.previousSibling; sib; sib = sib.previousSibling) {
if (sib.nodeType == 1 && sib.tagName == dom.tagName)
index++;
}
var xname = dom.tagName.toLowerCase();
if (dom.id) {
xname += "[@id="" + dom.id + ""]";
} else {
if (index > 0)
xname += "[" + index + "]";
}
path = "/" + xname + path;
}
path = path.replace("html[1]/body[1]/","html/body/");
return path;
};
o = []
$(`.sitemapPageName`).each((index, item) => {
const parents = $(item).parents(`ul`).parents(`.sitemapExpandableNode`).eq(0).find(`>div .sitemapPageName`)
const xpath = getXpath(item)
o.push({
name: $(item).text(),
parent: parents ? $(parents).text() : undefined,
xpath,
parentXpath: parents ? getXpath(parents[0]) : undefined,
})
})
o2 = o.map(item => {
return {
...item,
fullName: getName(item)
}
})
function getName(item){
let name = item.name
if(item.parent) {
const parentName = getName(o.find(item2 => item2.xpath === item.parentXpath))
name = `${parentName} -> ${name}`
}
return name
}
console.log(o)
console.log(o2)
str = o2.map(item => item.fullName).join(`
`)
console.log(str)