音频不会被JavaScript静音-需要使用mutationobserver删除音频标签 [英] Audio won't be muted with JavaScript - removing audio tags with mutationobserver needed

查看:105
本文介绍了音频不会被JavaScript静音-需要使用mutationobserver删除音频标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下用户脚本删除某个网站上的所有音频:

I tried to use the following userscript to remove all audio from a certain website:

// ==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.
});

此代码无法正常工作,我认为观察所有随机出现的audio标签并更改DOM正是我需要更好地解决的问题.

This code didn't work and I assume that observing all audio tags coming randomly and mutating the DOM is exactly what I need to better cope with this.

如何编写此突变观察者?我从来没有写过一个变异观察者,我觉得这个例子将非常简短,非常基础,正是我所需要的,才能体会到我刚刚描述的逻辑的代码上下文,并且我要深深地感谢任何愿意这样做的人.尝试向我和其他有类似问题的人展示它.

How can this mutation observer be written? I have never written a mutation observer and I feel as if this example would be very short and very basic and exactly what I need to get a taste of the code context of the logic which I just described and I would thank dearly to anyone who will try to show it to me and other people who are having a similar problem.

推荐答案

  • 枚举mutations和每个突变的addedNodes
  • 枚举节点的子元素,因为使用超快getElementsByTagName而不是超慢querySelectorAll可以在页面加载期间合并突变.
  • 请勿使用@grant none,它会在页面上下文中运行用户脚本,除非您确实确实需要直接访问页面的JavaScript对象
  • 在页面加载期间使用@run-at document-start使音频静音
    • Enumerate mutations and each mutation's addedNodes
    • Enumerate node's child elements because mutations are coalesced during page loading using the superfast getElementsByTagName instead of the superslow querySelectorAll
    • Don't use @grant none, which runs your userscript in the page context, unless you really need direct access to the page's JavaScript objects
    • Use @run-at document-start to mute the audio during page loading
    • // ==UserScript==
      // @name     addicto
      // @include  *
      // @run-at   document-start
      // ==/UserScript==
      
      const sites = ['mako.co.il'];
      if (sites.some(site => location.hostname.includes(site))) {
        new MutationObserver(mutations => {
          for (const m of mutations) {
            for (const node of m.addedNodes) {
              if (node.localName == 'audio') {
                audio.muted = true;
              } else if (node.firstElementChild) {
                for (const child of node.getElementsByTagName('audio')) {
                  audio.muted = true;
                }
              }
            }
          }
        }).observe(document, {subtree: true, childList: true});
      }
      

      这篇关于音频不会被JavaScript静音-需要使用mutationobserver删除音频标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆