Use mutation listener on desktop too

This commit is contained in:
Ajay Ramachandran
2021-06-09 14:57:53 -04:00
parent dacf766b4a
commit 315901f69b

View File

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