Skip to content

Commit

Permalink
fix: video element detection
Browse files Browse the repository at this point in the history
updated conditions of detecting video elements
  • Loading branch information
kurtnettle committed Nov 19, 2023
1 parent 14b39ca commit 0e6992b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/css_selectors.js
Original file line number Diff line number Diff line change
@@ -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'];
5 changes: 2 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -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;
}
}

Expand Down
20 changes: 18 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
});

0 comments on commit 0e6992b

Please sign in to comment.