Remove lookbehind because safari

Resolves https://github.com/ajayyy/SponsorBlock/issues/1626
This commit is contained in:
Ajay 2022-12-26 16:28:17 -05:00
parent 06a112a030
commit 27f3ced338

View file

@ -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,