diff options
author | Ajay <[email protected]> | 2022-07-12 00:59:13 -0400 |
---|---|---|
committer | Ajay <[email protected]> | 2022-07-12 00:59:13 -0400 |
commit | 60d106fc52851c0e7751cf8142fecfe635215db5 (patch) | |
tree | c43416465321665358b9122e7c7b8294d37b045d | |
parent | a4df2eab8f05bb3fe5f9e71f36f7e1fe9dcad99f (diff) | |
download | SponsorBlock-60d106fc52851c0e7751cf8142fecfe635215db5.tar.gz SponsorBlock-60d106fc52851c0e7751cf8142fecfe635215db5.zip |
Fix cases with multiple segments starting at the exact same time
-rw-r--r-- | src/content.ts | 35 | ||||
-rw-r--r-- | src/utils/genericUtils.ts | 11 |
2 files changed, 43 insertions, 3 deletions
diff --git a/src/content.ts b/src/content.ts index fd78f564..1caaf907 100644 --- a/src/content.ts +++ b/src/content.ts @@ -569,6 +569,18 @@ function startSponsorSchedule(includeIntersectingSegments = false, currentTime?: openNotice: skipInfo.openNotice }); + for (const extra of skipInfo.extraIndexes) { + const extraSkip = skipInfo.array[extra]; + if (shouldSkip(extraSkip)) { + skipToTime({ + v: video, + skipTime: [extraSkip.scheduledTime, extraSkip.segment[1]], + skippingSegments: [extraSkip], + openNotice: skipInfo.openNotice + }); + } + } + if (utils.getCategorySelection(currentSkip.category)?.option === CategorySkipOption.ManualSkip || currentSkip.actionType === ActionType.Mute) { forcedSkipTime = skipTime[0] + 0.001; @@ -1192,13 +1204,30 @@ async function whitelistCheck() { * Returns info about the next upcoming sponsor skip */ function getNextSkipIndex(currentTime: number, includeIntersectingSegments: boolean, includeNonIntersectingSegments: boolean): - {array: ScheduledTime[], index: number, endIndex: number, openNotice: boolean} { + {array: ScheduledTime[], index: number, endIndex: number, extraIndexes: number[], openNotice: boolean} { + + const autoSkipSorter = (segment: ScheduledTime) => { + const skipOption = utils.getCategorySelection(segment.category)?.option; + if (skipOption === CategorySkipOption.AutoSkip + && segment.actionType === ActionType.Skip) { + return 0; + } else if (skipOption !== CategorySkipOption.ShowOverlay) { + return 1; + } else { + return 2; + } + } const { includedTimes: submittedArray, scheduledTimes: sponsorStartTimes } = getStartTimes(sponsorTimes, includeIntersectingSegments, includeNonIntersectingSegments); const { scheduledTimes: sponsorStartTimesAfterCurrentTime } = getStartTimes(sponsorTimes, includeIntersectingSegments, includeNonIntersectingSegments, currentTime, true, true); - const minSponsorTimeIndex = sponsorStartTimes.indexOf(Math.min(...sponsorStartTimesAfterCurrentTime)); + const minSponsorTimeIndexes = GenericUtils.indexesOf(sponsorStartTimes, Math.min(...sponsorStartTimesAfterCurrentTime)); + const minSponsorTimeIndex = minSponsorTimeIndexes.sort( + (a, b) => ((autoSkipSorter(submittedArray[a]) - autoSkipSorter(submittedArray[b])) + || (submittedArray[a].segment[1] - submittedArray[a].segment[0]) - (submittedArray[b].segment[1] - submittedArray[b].segment[0])))[0] ?? -1; + const extraIndexes = minSponsorTimeIndexes.filter((i) => i === minSponsorTimeIndex || autoSkipSorter(submittedArray[i]) !== 0); + const endTimeIndex = getLatestEndTimeIndex(submittedArray, minSponsorTimeIndex); const { includedTimes: unsubmittedArray, scheduledTimes: unsubmittedSponsorStartTimes } = @@ -1214,6 +1243,7 @@ function getNextSkipIndex(currentTime: number, includeIntersectingSegments: bool array: submittedArray, index: minSponsorTimeIndex, endIndex: endTimeIndex, + extraIndexes, // Segments at same time that need seperate notices openNotice: true }; } else { @@ -1221,6 +1251,7 @@ function getNextSkipIndex(currentTime: number, includeIntersectingSegments: bool array: unsubmittedArray, index: minUnsubmittedSponsorTimeIndex, endIndex: previewEndTimeIndex, + extraIndexes: [], // No manual things for unsubmitted openNotice: false }; } diff --git a/src/utils/genericUtils.ts b/src/utils/genericUtils.ts index 409dc6e9..ba22afc4 100644 --- a/src/utils/genericUtils.ts +++ b/src/utils/genericUtils.ts @@ -64,8 +64,17 @@ function hexToRgb(hex: string): {r: number, g: number, b: number} { } : null; } +/** + * List of all indexes that have the specified value + * https://stackoverflow.com/a/54954694/1985387 + */ +function indexesOf<T>(array: T[], value: T): number[] { + return array.map((v, i) => v === value ? i : -1).filter(i => i !== -1); +} + export const GenericUtils = { wait, getErrorMessage, - getLuminance + getLuminance, + indexesOf }
\ No newline at end of file |