aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/urlParser.ts
blob: 24f444ba4672099bb51a5895e64c1b76cbc4e45f (plain)
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

export function getStartTimeFromUrl(url: string): number {
    const urlParams = new URLSearchParams(url);
    const time = urlParams?.get('t') || urlParams?.get('time_continue');

    return urlTimeToSeconds(time);
}

export function urlTimeToSeconds(time: string): number {
    if (!time) {
        return 0;
    }

    const re = /(?:(\d{1,3})h)?(?:(\d{1,2})m)?(\d+)s?/;
    const match = re.exec(time);

    if (match) {
        const hours = parseInt(match[1] ?? '0', 10);
        const minutes = parseInt(match[2] ?? '0', 10);
        const seconds = parseInt(match[3] ?? '0', 10);

        return hours * 3600 + minutes * 60 + seconds;
    } else if (/\d+/.test(time)) {
        return parseInt(time, 10);
    } else {
        return 0;
    }
}