Fix starting segments when there is a t=? in the url

This commit is contained in:
Ajay Ramachandran
2021-10-02 17:13:30 -04:00
parent 5f34a777f1
commit 8605e23fbb
3 changed files with 59 additions and 2 deletions

26
src/utils/urlParser.ts Normal file
View File

@@ -0,0 +1,26 @@
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 = /(?:(?<hours>\d{1,3})h)?(?:(?<minutes>\d{1,2})m)?(?<seconds>\d+)s?/;
const match = re.exec(time);
if (match) {
const hours = parseInt(match.groups.hours ?? '0', 10);
const minutes = parseInt(match.groups.minutes ?? '0', 10);
const seconds = parseInt(match.groups.seconds ?? '0', 10);
return hours * 3600 + minutes * 60 + seconds;
} else if (/\d+/.test(time)) {
return parseInt(time, 10);
}
}