11if ( window . autoPauseInjected !== true ) {
22 const env = chrome . runtime ? chrome : browser ;
33 window . autoPauseInjected = true ;
4- let manuallyPaused = false ;
4+ const manuallyPausedVideos = new WeakMap ( ) ;
55 let automaticallyPaused = false ;
66
77 let options = {
@@ -55,7 +55,10 @@ if (window.autoPauseInjected !== true) {
5555 }
5656
5757 if ( ! options . manualPause ) {
58- manuallyPaused = false ;
58+ const videoElements = document . getElementsByTagName ( "video" ) ;
59+ for ( const element of videoElements ) {
60+ manuallyPausedVideos . delete ( element ) ;
61+ }
5962 automaticallyPaused = true ;
6063 }
6164
@@ -188,8 +191,8 @@ if (window.autoPauseInjected !== true) {
188191
189192 const iframeElements = document . getElementsByTagName ( "iframe" ) ;
190193
191- for ( let i = 0 ; i < iframeElements . length ; i ++ ) {
192- const iframe = iframeElements [ i ] ;
194+ for ( const element of iframeElements ) {
195+ const iframe = element ;
193196 try {
194197 if ( request . action === "stop" ) {
195198 iframe . contentWindow . postMessage (
@@ -207,41 +210,44 @@ if (window.autoPauseInjected !== true) {
207210 }
208211 }
209212
210- for ( let i = 0 ; i < videoElements . length ; i ++ ) {
213+ for ( const element of videoElements ) {
211214 try {
212- if ( request . action === "stop" && ! manuallyPaused ) {
215+ const video = element ;
216+ const isManuallyPaused = manuallyPausedVideos . get ( video ) ;
217+
218+ if ( request . action === "stop" && ! isManuallyPaused ) {
213219 automaticallyPaused = true ;
214- videoElements [ i ] . pause ( ) ;
220+ video . pause ( ) ;
215221 } else if (
216222 request . action === "resume" &&
217- videoElements [ i ] . paused &&
218- ! manuallyPaused
223+ video . paused &&
224+ ! isManuallyPaused
219225 ) {
220226 debugLog (
221- `Attempting to resume video: paused=${ videoElements [ i ] . paused } , manuallyPaused=${ manuallyPaused } ` ,
227+ `Attempting to resume video: paused=${ video . paused } , manuallyPaused=${ isManuallyPaused } ` ,
222228 ) ;
223229 automaticallyPaused = false ;
224- if ( ! videoElements [ i ] . ended ) {
225- await videoElements [ i ] . play ( ) ;
230+ if ( ! video . ended ) {
231+ await video . play ( ) ;
226232 }
227233 } else if ( request . action === "resume" ) {
228234 debugLog (
229- `Resume blocked: paused=${ videoElements [ i ] . paused } , manuallyPaused=${ manuallyPaused } ` ,
235+ `Resume blocked: paused=${ video . paused } , manuallyPaused=${ isManuallyPaused } ` ,
230236 ) ;
231237 } else if ( request . action === "toggle_mute" ) {
232- videoElements [ i ] . muted = ! videoElements [ i ] . muted ;
238+ video . muted = ! video . muted ;
233239 } else if ( request . action === "mute" ) {
234- videoElements [ i ] . muted = true ;
240+ video . muted = true ;
235241 } else if ( request . action === "unmute" ) {
236- videoElements [ i ] . muted = false ;
242+ video . muted = false ;
237243 } else if ( request . action === "toggle" ) {
238- if ( videoElements [ i ] . paused && ! manuallyPaused ) {
239- if ( ! videoElements [ i ] . ended ) {
240- await videoElements [ i ] . play ( ) ;
244+ if ( video . paused && ! isManuallyPaused ) {
245+ if ( ! video . ended ) {
246+ await video . play ( ) ;
241247 }
242248 automaticallyPaused = false ;
243- } else if ( ! manuallyPaused ) {
244- videoElements [ i ] . pause ( ) ;
249+ } else if ( ! isManuallyPaused ) {
250+ video . pause ( ) ;
245251 automaticallyPaused = true ;
246252 }
247253 }
@@ -268,15 +274,15 @@ if (window.autoPauseInjected !== true) {
268274 ) ;
269275 if ( ! automaticallyPaused && options . manualPause ) {
270276 debugLog ( `Manually paused video` ) ;
271- manuallyPaused = true ;
277+ manuallyPausedVideos . set ( videoElement , true ) ;
272278 automaticallyPaused = false ;
273279 }
274280 } ) ;
275281
276282 videoElement . addEventListener ( "play" , ( _e ) => {
277283 if ( options . manualPause ) {
278284 debugLog ( `Manually resumed video` ) ;
279- manuallyPaused = false ;
285+ manuallyPausedVideos . delete ( videoElement ) ;
280286 }
281287 automaticallyPaused = false ;
282288 } ) ;
@@ -289,12 +295,14 @@ if (window.autoPauseInjected !== true) {
289295 if ( ! options . scrollpause ) {
290296 return ;
291297 }
292- if ( entries [ 0 ] . isIntersecting === true ) {
293- debugLog ( `Video not anymore in viewport` ) ;
294- sendMessage ( { visible : true } ) ;
295- } else {
296- debugLog ( `Video in viewport` ) ;
297- sendMessage ( { visible : false } ) ;
298+ for ( const entry of entries ) {
299+ if ( entry . isIntersecting === true ) {
300+ debugLog ( `Video in viewport` ) ;
301+ sendMessage ( { visible : true } ) ;
302+ } else {
303+ debugLog ( `Video scrolled out of viewport` ) ;
304+ sendMessage ( { visible : false } ) ;
305+ }
298306 }
299307 } ,
300308 { threshold : [ 0 ] } ,
@@ -305,8 +313,8 @@ if (window.autoPauseInjected !== true) {
305313 let videoElements = document . getElementsByTagName ( "video" ) ;
306314 sendMessage ( { hasVideos : hasVideos ( ) } ) ;
307315
308- for ( let i = 0 ; i < videoElements . length ; i ++ ) {
309- attachVideoListeners ( videoElements [ i ] , intersection_observer ) ;
316+ for ( const element of videoElements ) {
317+ attachVideoListeners ( element , intersection_observer ) ;
310318 }
311319 }
312320
@@ -329,8 +337,8 @@ if (window.autoPauseInjected !== true) {
329337 const videos = node . getElementsByTagName ( "video" ) ;
330338 if ( videos . length > 0 ) {
331339 debugLog ( `New video elements detected in added node` ) ;
332- for ( let i = 0 ; i < videos . length ; i ++ ) {
333- attachVideoListeners ( videos [ i ] , intersection_observer ) ;
340+ for ( const element of videos ) {
341+ attachVideoListeners ( element , intersection_observer ) ;
334342 }
335343 sendMessage ( { hasVideos : hasVideos ( ) } ) ;
336344 }
0 commit comments