diff --git a/src/content.ts b/src/content.ts index 33718929..51b03886 100644 --- a/src/content.ts +++ b/src/content.ts @@ -812,17 +812,7 @@ async function sponsorsLookup(id: string, keepOldSubmissions = true) { updatePreviewBar(); } - // Add existing chapters if we can - if (Config.config.renderAsChapters) { - GenericUtils.wait(() => getExistingChapters(sponsorVideoID, video.duration), - 5000, 100, (c) => c?.length > 0).then((chapters) => { - if (!existingChaptersImported && chapters?.length > 0) { - sponsorTimes = sponsorTimes.concat(...chapters); - existingChaptersImported = true; - updatePreviewBar(); - } - }); - } + importExistingChapters(true); } else if (response?.status === 404) { retryFetch(); } @@ -830,6 +820,20 @@ async function sponsorsLookup(id: string, keepOldSubmissions = true) { lookupVipInformation(id); } +function importExistingChapters(wait: boolean) { + if (Config.config.renderAsChapters && !existingChaptersImported + && (sponsorTimes?.length > 0 || sponsorTimesSubmitting.length > 0)) { + GenericUtils.wait(() => video && getExistingChapters(sponsorVideoID, video.duration), + wait ? 5000 : 0, 100, (c) => c?.length > 0).then((chapters) => { + if (!existingChaptersImported && chapters?.length > 0) { + sponsorTimes = (sponsorTimes ?? []).concat(...chapters); + existingChaptersImported = true; + updatePreviewBar(); + } + }); + } +} + function getEnabledActionTypes(): ActionType[] { const actionTypes = [ActionType.Skip, ActionType.Poi, ActionType.Chapter]; if (Config.config.muteSegments) { @@ -1586,6 +1590,8 @@ function startOrEndTimingNewSegment() { updateEditButtonsOnPlayer(); updateSponsorTimesSubmitting(false); + + importExistingChapters(false); } function getIncompleteSegment(): SponsorTime { @@ -1628,6 +1634,10 @@ function updateSponsorTimesSubmitting(getFromConfig = true) { source: segmentTime.source }); } + + if (sponsorTimesSubmitting.length > 0) { + importExistingChapters(true); + } } updatePreviewBar();