-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateJson.js
42 lines (30 loc) · 1.14 KB
/
generateJson.js
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
export function generateJson (page) {
return page.evaluate(() => {
function parseHTMLTableElem (tableEl) {
const columns = Array.from(tableEl.querySelectorAll('tr:first-child > td')).map(it => it.textContent)
const rows = tableEl.querySelectorAll('tr:not(:first-child)')
let categoryIndex = -1
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
const result = days.reduce((prev, cur) => ({ ...prev, [cur]: [] }), {})
for (const row of Array.from(rows)) {
const cells = Array.from(row.querySelectorAll('td'))
if (cells[0].classList.contains('darkbold')) {
categoryIndex++
continue
}
const isEmpty = !cells[1].textContent.trim()
if (isEmpty) {
continue
}
result[days[categoryIndex]].push(columns.reduce((obj, col, idx) => {
obj[col] = cells[idx].textContent
return obj
}
, {}))
}
return { days: result }
}
console.clear()
return parseHTMLTableElem(document.querySelector('#divTT > table:nth-child(2)'))
})
}