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-08-15 提交的版本,檢視 最新版本

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

// 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);
    mutePlayer();
  }
}

// (un)Mute Player
function mutePlayer()
{
  if (document.getElementsByClassName("player-button--volume").length >= 1)
  {
    document.getElementsByClassName("player-button--volume")[0].click();
    _tmuteVars.playerMuted = !(_tmuteVars.playerMuted);

    if (_tmuteVars.playerMuted === true)
    {
      _tmuteVars.adsDisplayed++;
      console.log("Ad #" + _tmuteVars.adsDisplayed + " detected. Player muted.");
      if (_tmuteVars.disableDisplay === true) document.getElementsByClassName("player-video")[0].style.visibility = "hidden";
    } else {
      console.log("Ad #" + _tmuteVars.adsDisplayed + " finished. 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或关注我们的公众号极客氢云获取最新地址