Twitch - Mute ads and optionally hide them

Automatically mutes the Twitch player when an advertisement started and unmute it once finished. You can also hide ads by setting disableDisplay to true.

当前为 2018-09-14 提交的版本,查看 最新版本

// ==UserScript==
// @name        Twitch - Mute ads and optionally hide them
// @namespace   TWITCHADS
// @description Automatically mutes the Twitch player when an advertisement started and unmute it once finished. You can also hide ads by setting disableDisplay to true.
// @include     https://www.twitch.tv/*
// @include     https://twitch.tv/*
// @version     1.04
// @license     MIT
// @author      Harest
// @grant       none
// ==/UserScript==
var _tmuteVars = { "timerCheck": 1000, // Checking rate of ad in progress
                  "playerMuted": false, // Player muted or not
                  "adsDisplayed": 0, // Number of ads displayed
                  "disableDisplay": false, // Disable the player display during an ad (true = yes, false = no (default))
                  "alreadyMuted": false // Used to check if the player is muted at the start of an ad
                 };

// Check if there's an ad
function checkAd()
{
  var advert = document.getElementsByClassName('player-ad-notice'); // class "twitch-stitched-ad" doesn't seem to appear
  if ((advert.length >= 1 && _tmuteVars.playerMuted === false) || (_tmuteVars.playerMuted === true && advert.length === 0)) 
  {
    console.log(advert);
    // Update at the start of an ad if the player is already muted or not
    if (advert.length >= 1) _tmuteVars.alreadyMuted = Boolean(document.getElementsByClassName("unmute-button").length); 
    
    mutePlayer();
  }
}

// (un)Mute Player
function mutePlayer()
{
  if (document.getElementsByClassName("player-button--volume").length >= 1)
  {
    if (_tmuteVars.alreadyMuted === false) document.getElementsByClassName("player-button--volume")[0].click(); // If the player is already muted before an ad, we avoid to unmute it.
    _tmuteVars.playerMuted = !(_tmuteVars.playerMuted);

    if (_tmuteVars.playerMuted === true)
    {
      _tmuteVars.adsDisplayed++;
      console.log("Ad #" + _tmuteVars.adsDisplayed + " detected. Player " + (_tmuteVars.alreadyMuted === true ? "already " : "") + "muted.");
      if (_tmuteVars.disableDisplay === true) document.getElementsByClassName("player-video")[0].style.visibility = "hidden";
    } else {
      console.log("Ad #" + _tmuteVars.adsDisplayed + " finished." + (_tmuteVars.alreadyMuted === true ? "" : " Player unmuted."));
      if (_tmuteVars.disableDisplay === true) document.getElementsByClassName("player-video")[0].style.visibility = "visible";
    }
  } else {
    console.log("No volume button found (class changed ?).");
  }
}

_tmuteVars.autoCheck = setInterval(checkAd, _tmuteVars.timerCheck);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址