Skip to content

Commit 87b8d1e

Browse files
shakyShaneShane Osbourne
and
Shane Osbourne
authored
fix(duckplayer): prevent stale references to DOM nodes (#595)
Co-authored-by: Shane Osbourne <[email protected]>
1 parent 4f85178 commit 87b8d1e

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/features/duckplayer/video-overlay-manager.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export class VideoOverlayManager {
1515
/** @type {import("./video-player-icon").VideoPlayerIcon | null} */
1616
videoPlayerIcon = null
1717

18+
selectors = {
19+
videoElement: '#player video',
20+
container: '#player .html5-video-player'
21+
}
22+
1823
/**
1924
* @param {import("../duck-player.js").UserValues} userValues
2025
* @param {import("./overlays.js").Environment} environment
@@ -80,7 +85,7 @@ export class VideoOverlayManager {
8085
* @param {import("./util").VideoParams} params
8186
*/
8287
addSmallDaxOverlay (params) {
83-
const containerElement = document.querySelector('#player .html5-video-player')
88+
const containerElement = document.querySelector(this.selectors.container)
8489
if (!containerElement) {
8590
console.error('no container element')
8691
return
@@ -117,17 +122,11 @@ export class VideoOverlayManager {
117122
]
118123

119124
if (conditions.some(Boolean)) {
120-
const playerElement = document.querySelector('#player')
121-
122-
if (!playerElement) {
123-
return null
124-
}
125-
126125
/**
127126
* Don't continue until we've been able to find the HTML elements that we inject into
128127
*/
129-
const videoElement = playerElement.querySelector('video')
130-
const playerContainer = playerElement.querySelector('.html5-video-player')
128+
const videoElement = document.querySelector(this.selectors.videoElement)
129+
const playerContainer = document.querySelector(this.selectors.container)
131130
if (!videoElement || !playerContainer) {
132131
return null
133132
}
@@ -152,7 +151,7 @@ export class VideoOverlayManager {
152151
if ('alwaysAsk' in userValues.privatePlayerMode) {
153152
if (!userValues.overlayInteracted) {
154153
if (!this.environment.hasOneTimeOverride()) {
155-
this.stopVideoFromPlaying(videoElement)
154+
this.stopVideoFromPlaying()
156155
this.appendOverlayToPage(playerContainer, params)
157156
}
158157
} else {
@@ -187,15 +186,16 @@ export class VideoOverlayManager {
187186
/**
188187
* Just brute-force calling video.pause() for as long as the user is seeing the overlay.
189188
*/
190-
stopVideoFromPlaying (videoElement) {
191-
this.sideEffect('pausing the <video> element', () => {
189+
stopVideoFromPlaying () {
190+
this.sideEffect(`pausing the <video> element with selector '${this.selectors.videoElement}'`, () => {
192191
/**
193192
* Set up the interval - keep calling .pause() to prevent
194193
* the video from playing
195194
*/
196195
const int = setInterval(() => {
197-
if (videoElement instanceof HTMLVideoElement && videoElement.isConnected) {
198-
videoElement.pause()
196+
const video = /** @type {HTMLVideoElement} */(document.querySelector(this.selectors.videoElement))
197+
if (video?.isConnected) {
198+
video.pause()
199199
}
200200
}, 10)
201201

@@ -206,13 +206,9 @@ export class VideoOverlayManager {
206206
return () => {
207207
clearInterval(int)
208208

209-
if (videoElement?.isConnected) {
210-
videoElement.play()
211-
} else {
212-
const video = document.querySelector('#player video')
213-
if (video instanceof HTMLVideoElement) {
214-
video.play()
215-
}
209+
const video = /** @type {HTMLVideoElement} */(document.querySelector(this.selectors.videoElement))
210+
if (video?.isConnected) {
211+
video.play()
216212
}
217213
}
218214
})

0 commit comments

Comments
 (0)