diff --git a/src/content.ts b/src/content.ts index 4822eff1..c1c334ed 100644 --- a/src/content.ts +++ b/src/content.ts @@ -35,7 +35,6 @@ let channelIDInfo: ChannelIDInfo; // Skips are rescheduled every seeking event. // Skips are canceled every seeking event let currentSkipSchedule: NodeJS.Timeout = null; -let seekListenerSetUp = false /** Has the sponsor been skipped */ let sponsorSkipped: boolean[] = []; @@ -44,6 +43,7 @@ let sponsorSkipped: boolean[] = []; let video: HTMLVideoElement; let videoMutationObserver: MutationObserver = null; // List of videos that have had event listeners added to them +const videosWithEventListeners: HTMLVideoElement[] = []; const videoRootsWithEventListeners: HTMLDivElement[] = []; let onInvidious; @@ -475,17 +475,11 @@ function incorrectVideoCheck(videoID?: string, sponsorTime?: SponsorTime): boole } } -function setupMobileVideoMutationListener() { +function setupVideoMutationListener() { const videoContainer = document.querySelector(".html5-video-container"); - if (!videoContainer || videoMutationObserver !== null) return; - - videoMutationObserver = new MutationObserver(() => { - const newVideo = document.querySelector('video'); - if (newVideo && newVideo !== video) { - video = newVideo; - setupVideoListeners(); - } - }); + if (!videoContainer || videoMutationObserver !== null || onInvidious) return; + + videoMutationObserver = new MutationObserver(refreshVideoAttachments); videoMutationObserver.observe(videoContainer, { attributes: true, @@ -494,11 +488,24 @@ function setupMobileVideoMutationListener() { }); } +function refreshVideoAttachments() { + const newVideo = document.querySelector('video'); + if (newVideo && newVideo !== video) { + video = newVideo; + + addHotkeyListener(); + if (!videosWithEventListeners.includes(video)) { + videosWithEventListeners.push(video); + + setupVideoListeners(); + } + } +} + function setupVideoListeners() { //wait until it is loaded video.addEventListener('durationchange', durationChangeListener); - if (!Config.config.disableSkipping) { switchingVideos = false; @@ -559,21 +566,14 @@ function setupVideoListeners() { } async function sponsorsLookup(id: string) { - video = document.querySelector('video'); // Youtube video player - //there is no video here - if (video == null) { + if (!video) refreshVideoAttachments(); + //there is still no video here + if (!video) { setTimeout(() => sponsorsLookup(id), 100); return; } - if (onMobileYouTube) setupMobileVideoMutationListener(); - - addHotkeyListener(); - - if (!seekListenerSetUp && !Config.config.disableSkipping) { - seekListenerSetUp = true; - setupVideoListeners(); - } + setupVideoMutationListener(); //check database for sponsor times //made true once a setTimeout has been created to try again after a server error