1

I tried to use this code I found here in StackExchange to prevent advertisements sound on certain websites.

Array.prototype.slice.call(document.querySelectorAll('audio')).forEach((audio)=>{
    audio.muted = true;
});

I implemented the code this way:

// ==UserScript==
// @name        addicto
// @namespace   nms
// @include     http://*
// @include     https://*
// @version     1
// @grant       none
// ==/UserScript==

addEventListener('DOMContentLoaded', ()=>{
  let sites = ['mako.co.il'];
  let href = window.location.href;
  for (let i = 0; i < sites.length; i++) {
    if (href.includes(sites[i])) {
      Array.prototype.slice.call(document.querySelectorAll('audio')).forEach((audio)=>{
        audio.muted = true;
      });
    }
  }

  // If href includes the value of the iteration on the "sites" array, do stuff.
});

This didn't work and I still hear sounds from the site (mako.co.il). Maybe this is because the code doesn't work when the sound is coming from advertisement scripts?

If so, maybe a decent solution would be to disable any other script beside my scripts on the desired website? or maybe something else?

fayalikt
  • 141
  • 1
  • 15
  • You DOM hasn’t loaded yet, when you run that script, most likely. Wrap that in a `DOMContentLoaded` event listener. – Sebastian Simon Oct 19 '17 at 05:32
  • @Xufox I just tried that. Sadly, didn't work... I've updated the question with the exact code. – fayalikt Oct 19 '17 at 06:42
  • In that case, those ` – Sebastian Simon Oct 19 '17 at 07:30
  • The meaning here is that I should "observe for DOM mutations involving addition of audio tags related to the video in the advertisement and response with removing them", right? – fayalikt Oct 19 '17 at 07:36
  • 1
    That’s pretty much it, yes. – Sebastian Simon Oct 19 '17 at 07:39
  • `document.addEventListener`? Keep in mind, ads especially are loaded later, often after the page content is loaded. They can also be in iframes. – Brad Oct 19 '17 at 17:58
  • @Brad, is there a "general" way to listen to all audio tags in an efficient way? Something more efficient than a 10 milliseconds `setInterval`? (of course, I should learn of `mutationObserver`. – fayalikt Oct 20 '17 at 03:24
  • Why not make a browser extension and mute the tab? – Brad Oct 20 '17 at 04:02
  • Muting the tab manually each time is a pain, I'm sure you meant something else. What is it? – fayalikt Oct 20 '17 at 04:07

0 Answers0