Skip to content

Commit 360e581

Browse files
authored
Duck Player Native: Mute all elements (#1779)
1 parent c12a146 commit 360e581

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

injected/src/features/duckplayer-native/pause-video.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,41 @@ export function stopVideoFromPlaying(videoSelector) {
2929
}
3030
};
3131
}
32+
33+
const MUTE_ELEMENTS_QUERY = 'audio, video';
34+
35+
/**
36+
* Mute all audio and video elements
37+
*
38+
* @returns {() => void} A function that allows the elements to be unmuted
39+
*/
40+
export function muteAllElements() {
41+
/**
42+
* Set up the interval
43+
*/
44+
const int = setInterval(() => {
45+
/** @type {(HTMLAudioElement | HTMLVideoElement)[]} */
46+
const elements = Array.from(document.querySelectorAll(MUTE_ELEMENTS_QUERY));
47+
elements.forEach((element) => {
48+
if (element?.isConnected) {
49+
element.muted = true;
50+
}
51+
});
52+
}, 10);
53+
54+
/**
55+
* To clean up, we need to stop the interval
56+
* and then call .play() on the original element, if it's still connected
57+
*/
58+
return () => {
59+
clearInterval(int);
60+
61+
/** @type {(HTMLAudioElement | HTMLVideoElement)[]} */
62+
const elements = Array.from(document.querySelectorAll(MUTE_ELEMENTS_QUERY));
63+
elements.forEach((element) => {
64+
if (element?.isConnected) {
65+
element.muted = false;
66+
}
67+
});
68+
};
69+
}

injected/src/features/duckplayer-native/sub-features/duck-player-native-youtube.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Logger, SideEffects } from '../../duckplayer/util.js';
22
import { muteAudio } from '../mute-audio.js';
33
import { pollTimestamp } from '../get-current-timestamp.js';
4-
import { stopVideoFromPlaying } from '../pause-video.js';
4+
import { stopVideoFromPlaying, muteAllElements } from '../pause-video.js';
55
import { showThumbnailOverlay } from '../overlays/thumbnail-overlay.js';
66

77
/**
@@ -86,6 +86,7 @@ export class DuckPlayerNativeYoutube {
8686

8787
if (pause) {
8888
this.sideEffects.add('stopping video from playing', () => stopVideoFromPlaying(videoElement));
89+
this.sideEffects.add('muting all elements', () => muteAllElements());
8990
this.sideEffects.add('appending thumbnail', () => {
9091
const clickHandler = () => {
9192
this.messages.notifyOverlayDismissed();
@@ -95,6 +96,7 @@ export class DuckPlayerNativeYoutube {
9596
});
9697
} else {
9798
this.sideEffects.destroy('stopping video from playing');
99+
this.sideEffects.destroy('muting all elements');
98100
this.sideEffects.destroy('appending thumbnail');
99101
}
100102
}

0 commit comments

Comments
 (0)