mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-11 22:17:21 +03:00
Fix starting segments when there is a t=? in the url
This commit is contained in:
26
src/utils/urlParser.ts
Normal file
26
src/utils/urlParser.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user