YouTube - Mute Ads

Automatically mutes VideoAds

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name                YouTube - Mute Ads
// @name:de             YouTube - Mute Ads
// @version             2.0.0
// @description         Automatically mutes VideoAds
// @description:de      Schaltet Werbung auf YouTube automatisch stumm
// @autor               VVind0wM4ker
// @namespace           https://github.com/VVind0wM4ker/Userscripts
// @homepageURL         https://github.com/VVind0wM4ker/Userscripts/tree/master/YouTube_Mute_Ads
// @license             MIT License
// @grant               none
// @noframes
// @include             http*://*.youtube.com/watch*
// ==/UserScript==

var adHandled = false;
var playerMutedBefore;

// ----- Setters and Getters -----
// Getter functions instead of vars to prevent getting old elements
// in case of navigation for example
function getVideo() {
  return document.getElementsByClassName("video-stream html5-main-video")[0];
}
function getPlayer() {
  return document.getElementsByClassName("html5-video-player")[0];
}
function getMuteBtn() {
  if (getPlayer()) {
    return getPlayer().querySelector("button.ytp-mute-button");
  }
  return null;
}
function isAdInterrupting() {
  if (getPlayer()) {
    return getPlayer().className.indexOf("ad-interrupting");
  }
  return null;
}
// -------------------------------

function hook() {
  // add eventlistener if userscript started before the site finished loading
  if (document.readyState == "loading") {
    document.addEventListener("DOMContentLoaded", function() {hook();});
    return;
  }
  // site loaded

  // detect navigation on the site
  document.body.addEventListener("yt-navigate-finish", function() {setup();});
  setup();
}

function setup() {
  // prevent mess if setup() is called more than once
  if (getVideo().onplay === null) {
    getVideo().onplay = function() {analVideo();};
  }
}

// ( ͡° ͜ʖ ͡°)
function analVideo() {
  if (isAdInterrupting() !== -1 && adHandled === false ) {
    adHandled = true;
    playerMutedBefore = getPlayer().isMuted();
    getPlayer().mute();
  }
  else if (isAdInterrupting() === -1 && adHandled === true) {
      adHandled = false;
      if (playerMutedBefore === false) {
        getPlayer().unMute();
      } else {
        getPlayer().mute();
      }
  }
}

hook();