Repository navigation
Expand file tree
/
Copy pathexctractor.js
More file actions
66 lines (52 loc) · 1.85 KB
/
Copy pathexctractor.js
File metadata and controls
66 lines (52 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Categories tree builder for YGG
// If you need to update the tree, go te the home page and paste it in the console
function cleanText(text) {
return text
.replace(/[\n\t]/g, ' ')
.replace(/\s+/g, ' ')
.replace(/ /g, ' ')
.trim();
}
function parseCategories(rootElement) {
const categories = [];
const parentItems = rootElement.querySelectorAll(':scope > li');
parentItems.forEach(parentLi => {
const subUl = parentLi.querySelector(':scope > ul');
if (!subUl) return;
const parentLink = subUl.querySelector(':scope > li:first-child > a');
if (!parentLink) return;
const parentUrl = new URL(parentLink.href);
const parentParams = new URLSearchParams(parentUrl.search);
const category = {
id: parentParams.get('category') || parentLi.className,
name: cleanText(parentLink.textContent),
sub_categories: []
};
subUl.querySelectorAll(':scope > li:not(:first-child)').forEach(subLi => {
const subLink = subLi.querySelector('a');
if (!subLink) return;
const subUrl = new URL(subLink.href);
const subParams = new URLSearchParams(subUrl.search);
category.sub_categories.push({
id: subParams.get('sub_category') || '',
name: cleanText(subLink.textContent),
sub_categories: []
});
});
categories.push(category);
});
return categories;
}
const targetElement = document.evaluate(
'/html/body/div[7]/ul',
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
if (targetElement) {
const categoriesTree = parseCategories(targetElement);
console.log(JSON.stringify(categoriesTree, null, 2));
} else {
console.error('Élément non trouvé');
}