diff options
author | Ajay <[email protected]> | 2022-12-26 16:28:17 -0500 |
---|---|---|
committer | Ajay <[email protected]> | 2022-12-26 16:28:17 -0500 |
commit | 27f3ced338f9ff7333f0126e556c3f114eaeb0f9 (patch) | |
tree | 31675cf75b336c25a7448c153daa85db05c11b96 /src/utils | |
parent | 06a112a0302c7193f6035d5e1521a331721932db (diff) | |
download | SponsorBlock-27f3ced338f9ff7333f0126e556c3f114eaeb0f9.tar.gz SponsorBlock-27f3ced338f9ff7333f0126e556c3f114eaeb0f9.zip |
Remove lookbehind because safari
Resolves https://github.com/ajayyy/SponsorBlock/issues/1626
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/exporter.ts | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/utils/exporter.ts b/src/utils/exporter.ts index 3c2ca6f9..9c809b41 100644 --- a/src/utils/exporter.ts +++ b/src/utils/exporter.ts @@ -41,11 +41,16 @@ export function importTimes(data: string, videoDuration: number): SponsorTime[] const startTime = getFormattedTimeToSeconds(match[0]); if (startTime !== null) { // Remove "seconds", "at", special characters, and ")" if there was a "(" - const specialCharsMatcher = /^(?:\s+seconds?)?[-:()\s]*|(?:\s+at)?[-:(\s]+$|(?<=^\s*\(.+)[-:()\s]*$/g - const titleLeft = line.split(match[0])[0].replace(specialCharsMatcher, ""); + const specialCharMatchers = [{ + matcher: /^(?:\s+seconds?)?[-:()\s]*|(?:\s+at)?[-:(\s]+$/g + }, { + matcher: /[-:()\s]*$/g, + condition: (value) => !!value.match(/^\s*\(/) + }]; + const titleLeft = removeIf(line.split(match[0])[0], specialCharMatchers); let titleRight = null; const split2 = line.split(match[1] || match[0]); - titleRight = split2[split2.length - 1].replace(specialCharsMatcher, ""); + titleRight = removeIf(split2[split2.length - 1], specialCharMatchers) const title = titleLeft?.length > titleRight?.length ? titleLeft : titleRight; if (title) { @@ -77,6 +82,17 @@ export function importTimes(data: string, videoDuration: number): SponsorTime[] return result; } +function removeIf(value: string, matchers: Array<{ matcher: RegExp, condition?: (value: string) => boolean }>): string { + let result = value; + for (const matcher of matchers) { + if (!matcher.condition || matcher.condition(value)) { + result = result.replace(matcher.matcher, ""); + } + } + + return result; +} + export function exportTimesAsHashParam(segments: SponsorTime[]): string { const hashparamSegments = segments.map(segment => ({ actionType: segment.actionType, |