From 27f3ced338f9ff7333f0126e556c3f114eaeb0f9 Mon Sep 17 00:00:00 2001 From: Ajay Date: Mon, 26 Dec 2022 16:28:17 -0500 Subject: [PATCH] Remove lookbehind because safari Resolves https://github.com/ajayyy/SponsorBlock/issues/1626 --- src/utils/exporter.ts | 22 +++++++++++++++++++--- 1 file 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,