From 0e6992b49461881c7de441704ad742eb22578057 Mon Sep 17 00:00:00 2001 From: kurtnettle <89929240+KurtNettle@users.noreply.github.com> Date: Sun, 19 Nov 2023 11:36:24 +0600 Subject: [PATCH] fix: video element detection updated conditions of detecting video elements --- src/css_selectors.js | 1 + src/helpers.js | 5 ++--- src/main.js | 20 ++++++++++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/css_selectors.js b/src/css_selectors.js index af696d6..a455a79 100644 --- a/src/css_selectors.js +++ b/src/css_selectors.js @@ -1,3 +1,4 @@ export const D_VIDEO_DURATION_ITEMS = 'span.ytp-time-duration'; export const M_VIDEO_DURATION_ITEMS = 'span.time-second'; export const SPONSORBLOCKDURATION_ELEM = 'span#sponsorBlockDurationAfterSkips'; +export const VIDEO_CLASSLIST = ['video-stream', 'html5-main-video']; diff --git a/src/helpers.js b/src/helpers.js index 71288f2..c4cf460 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,10 +1,9 @@ -import { SPONSORBLOCKDURATION_ELEM } from './css_selectors.js'; +import { SPONSORBLOCKDURATION_ELEM, VIDEO_CLASSLIST } from './css_selectors.js'; export function getVideoElement () { const videos = document.getElementsByTagName('video'); for (const video of videos) { - if (video.getAttribute('src')) return video; - if (video.getAttribute('data-no-fullscreen') == true) return null; + if (VIDEO_CLASSLIST.every(_class => video.classList.contains(_class))) return video; } } diff --git a/src/main.js b/src/main.js index 4334b84..53b0a86 100644 --- a/src/main.js +++ b/src/main.js @@ -3,10 +3,20 @@ import { getVideoElement } from './helpers.js'; let hasAddedVideoListener = false; function addVideoListener (hasAddedVideoListener) { - if (hasAddedVideoListener) return; + if (hasAddedVideoListener) { + console.debug('[yt-pb-calc] [desktop] already added event listener.'); + return true; + } else { + console.debug('[yt-pb-calc] [desktop] adding event listener.'); + } const video = getVideoElement(); - if (video == null) return false; + if (video == null) { + console.debug('[yt-pb-calc] [desktop] failed to get video element.'); + return false; + } else { + console.debug('[yt-pb-calc] [desktop] got video element.'); + } video.addEventListener('ratechange', (event) => { setDurationText(video.playbackRate, video.duration); @@ -19,6 +29,12 @@ function addVideoListener (hasAddedVideoListener) { }; document.addEventListener('yt-navigate-finish', function (event) { + console.debug('[yt-pb-calc] [desktop] finish navigation'); hasAddedVideoListener = addVideoListener(hasAddedVideoListener); // } }); + +document.addEventListener('yt-navigate-start', function (event) { + console.debug('[yt-pb-calc] [desktop] start navigation'); + hasAddedVideoListener = false; +});