Is it possible to mute any sound from a tab via vanilla JavaScript?

Say I open a webpage like this one and start playing audio from it.

To stop the audio, this doesn’t work:

window.setInterval(()=>{
    if ( document.querySelector('audio') ) {
        document.querySelectorAll('audio').forEach(audio=>{
            audio.pause();
            audio.remove();
        });
    };
}, 1);

Other ways to mute the audio would be to just pause the audio player or to mute the tab from the browser by clicking on the tab mute button or CTRL+M:

But if I want to mute any sound whatsoever from a tab by vanilla JavaScript, is it possible?
If I would have needed to bet, I would have bet that it is not possible because the Window layer of JavaScript is one layer down from the Browser layer, but I might be wrong and it is indeed possible.

Use document.querySelectorAll('audio')

I did :blush: please re-check my code…

You do not need the ‘if’.

Try deleting audio.remove(); and changing the 1ms interval to say 5000ms and set an audio playing.

An audio element doesn’t have to be attached in order to play, so querying the document will indeed not work reliably. As a minimal example, you could also start playing like so:

new Audio('my-audio-url.mp3').addEventListener('canplay', event => event.target.play())

There is but only for browser extensions, e.g.:

https://developer.chrome.com/docs/extensions/reference/api/tabs#mute

The if is there is an insurance to prevent abrupt termination of a longer script file that does other things as well (such as removing images and videos), but to the main issue:

I don’t think it matters if the interval is set to 1 ms or 5,000 ms, but anyway such a code doesn’t prevent audio playing there:

window.setInterval(()=>{
        document.querySelectorAll('audio').forEach(audio=>{
            audio.pause();
        });
}, 1);

If you have an alternative code that worked for you, please do share it.

I think that the event listening approach here is indeed more reliable :slight_smile:

But let’s say that I don’t know what is the name of the audio file that is being played, I just want to stop all audio whatsoever on a webpage, however it is being played.

Is there a JavaScript way to do that automatically?

Pseudocode:

window.addEventListener( 'sound', event=> event.pause() )

I tested your code without audio.remove() on an <audio> player (within the HTML DOM) and it worked (with and without the ‘if’). However, I did increase the interval to make sure 1ms is not too short. Incresing the interval to 5000ms enabled me to confirm that the audio paused after about 5 seconds.

Have you ran this code:

window.setInterval(()=>{
        document.querySelectorAll('audio').forEach(audio=>{
            audio.pause();
        });
}, 5000);

On the page that I have originally linked to?

If so, I did the same, but the sound didn’t stop in Google Chrome version 129.0.6668.101 (Official Build) (64-bit).

I do not know how to run code on your web page.

I ran the code on this web page (audio source filename needs to be inserted):

<!DOCTYPE html>
<html lang="en">
<head>
<title>Audio Pause</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<audio controls src=""></audio>
</body>
<script>
window.setInterval(()=>{
  document.querySelectorAll('audio').forEach(audio=>{
    audio.pause();
	});
  }, 5000);
</script>
</html>

Sorry that’s a misunderstanding, I just wanted to demonstrate how you can play audio without having an audio element attached to the document. This means you can not stop it reliably from within another script.

As for listening to media events on the window, this won’t work either I’m afraid since these events (play, playing etc.) do not bubble.

Is there no way to pause all sound whatsoever from a tab via vanilla JavaScript?

By the way, I have asked ChatGPT the following question:

How to mute all sound in a web browser tab with JavaScript?

The answer (which they ask not to paste in other websites to not mislead their algorithm) mentioned only audio and video elements so ChatGPT failed to provide an answer about how to mute sound that doesn’t arrive from audio elements.

I draw your attention to the Web Audio API but I do not think it will help.

As mentioned, you might have a look at extension development. That’s still vanilla JS I guess, just another interface.

I don’t want to develop any browser extensions, our beloved @m3g4p0p :slight_smile:

I want to mute a tab directly from vanilla JS from the console.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.