@@ -10,76 +10,21 @@ import { EVENT_PLAY, EVENT_PAUSE } from '../components/PlaybackControl.jsx';
10
10
* @implements IframeFeature
11
11
*/
12
12
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
-
27
13
/**
28
14
* @param {HTMLIFrameElement } iframe
29
15
*/
30
16
iframeDidLoad ( iframe ) {
31
- const documentBody = iframe . contentWindow ?. document ?. body ;
17
+ const document = iframe . contentWindow ?. document ;
32
18
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 ( ) ;
61
20
window . addEventListener ( EVENT_PLAY , playHandler ) ;
62
- this . handlers . push ( [ EVENT_PLAY , playHandler ] ) ;
63
21
64
- const pauseHandler = ( ) => videoElement . pause ( ) ;
22
+ const pauseHandler = ( ) => document ?. querySelector ( 'video' ) ? .pause ( ) ;
65
23
window . addEventListener ( EVENT_PAUSE , pauseHandler ) ;
66
- this . handlers . push ( [ EVENT_PAUSE , pauseHandler ] ) ;
67
- }
68
24
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
+ } ;
84
29
}
85
30
}
0 commit comments