forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord.ts
More file actions
52 lines (48 loc) · 1.81 KB
/
record.ts
File metadata and controls
52 lines (48 loc) · 1.81 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
import { LaunchContext } from "./base"
type RecordRow = {
date: string
url: string
name: string
category: string
time: string
runTime?: string
visit: string
}
function readRecords(): RecordRow[] {
const rows = document.querySelectorAll('.el-table .el-table__body-wrapper table tbody tr')
return Array.from(rows).map(row => {
const cells = row.querySelectorAll('td')
const date = cells[1].textContent ?? ''
const url = cells[2].textContent ?? ''
const name = cells[3].textContent ?? ''
const category = cells[4].textContent ?? ''
const time = cells[5].textContent ?? ''
let runTime: string | undefined = undefined, visit = ''
if (cells?.length === 9) {
// Including run time
runTime = cells[6].textContent ?? undefined
visit = cells[7].textContent ?? ''
} else {
visit = cells[6].textContent ?? ''
}
return { date, url, name, category, time, runTime, visit }
})
}
export function parseTime2Sec(timeStr: string | undefined): number | undefined {
if (!timeStr || timeStr === '-') return undefined
const regRes = /^(\s*(?<hour>\d+)\s*h)?(\s*(?<min>\d+)\s*m)?(\s*(?<sec>\d+)\s*s)$/.exec(timeStr)
if (!regRes) return NaN
const { hour, min, sec } = regRes.groups || {}
let res = parseInt(sec ?? '0')
min && (res += 60 * parseInt(min))
hour && (res += 3600 * parseInt(hour))
return res
}
export async function readRecordsOfFirstPage(context: LaunchContext) {
const recordPage = await context.openAppPage('/data/report')
// At least one record
await recordPage.waitForSelector('.el-table .el-table__body-wrapper table tbody tr td')
let records = await recordPage.evaluate(readRecords)
await recordPage.close()
return records
}