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