Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cb4d651

Browse files
committedJan 29, 2025·
Simpler alternative. Thanks @shakeyShane
1 parent 7edce77 commit cb4d651

File tree

1 file changed

+7
-62
lines changed

1 file changed

+7
-62
lines changed
 

‎special-pages/pages/duckplayer/app/features/playback-events.js

+7-62
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,21 @@ import { EVENT_PLAY, EVENT_PAUSE } from '../components/PlaybackControl.jsx';
1010
* @implements IframeFeature
1111
*/
1212
export class PlaybackEvents {
13-
/** @type {MutationObserver} */
14-
observer;
15-
16-
/** @type {[EVENT_PLAY|EVENT_PAUSE, (() => void)][]} */
17-
handlers = [];
18-
19-
destroy() {
20-
if (this.observer) this.observer.disconnect();
21-
22-
this.handlers.forEach(([event, handler]) => {
23-
window.removeEventListener(event, handler);
24-
});
25-
}
26-
2713
/**
2814
* @param {HTMLIFrameElement} iframe
2915
*/
3016
iframeDidLoad(iframe) {
31-
const documentBody = iframe.contentWindow?.document?.body;
17+
const document = iframe.contentWindow?.document;
3218

33-
if (documentBody) {
34-
const videoElement = documentBody.querySelector('video');
35-
36-
// Check if iframe already contains video
37-
if (videoElement) {
38-
this.addHandlersToVideo(videoElement);
39-
} else {
40-
// No video found. Observe iframe's document for changes
41-
this.observer = new MutationObserver(this.handleMutation.bind(this));
42-
43-
this.observer.observe(documentBody, {
44-
childList: true,
45-
subtree: true, // Observe all descendants of the body
46-
});
47-
}
48-
}
49-
50-
return () => {
51-
this.destroy();
52-
};
53-
}
54-
55-
/**
56-
*
57-
* @param {HTMLVideoElement} videoElement
58-
*/
59-
addHandlersToVideo(videoElement) {
60-
const playHandler = () => videoElement.play();
19+
const playHandler = () => document?.querySelector('video')?.play();
6120
window.addEventListener(EVENT_PLAY, playHandler);
62-
this.handlers.push([EVENT_PLAY, playHandler]);
6321

64-
const pauseHandler = () => videoElement.pause();
22+
const pauseHandler = () => document?.querySelector('video')?.pause();
6523
window.addEventListener(EVENT_PAUSE, pauseHandler);
66-
this.handlers.push([EVENT_PAUSE, pauseHandler]);
67-
}
6824

69-
/**
70-
* Mutation handler that checks for a new video element
71-
*
72-
* @type {MutationCallback}
73-
*/
74-
handleMutation(mutationsList) {
75-
for (const mutation of mutationsList) {
76-
if (mutation.type === 'childList') {
77-
mutation.addedNodes.forEach((node) => {
78-
if (node.nodeType === Node.ELEMENT_NODE && /** @type {HTMLElement} */ (node).tagName === 'VIDEO') {
79-
this.addHandlersToVideo(/** @type {HTMLVideoElement} */ (node));
80-
}
81-
});
82-
}
83-
}
25+
return () => {
26+
window.removeEventListener(EVENT_PLAY, playHandler);
27+
window.removeEventListener(EVENT_PAUSE, pauseHandler);
28+
};
8429
}
8530
}

0 commit comments

Comments
 (0)
Please sign in to comment.