Twitch - Mute ads

Automatically mutes the Twitch player when an advertisement started and unmute it once finished.

当前为 2018-08-15 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Twitch - Mute ads
// @namespace   TWITCHADS
// @description Automatically mutes the Twitch player when an advertisement started and unmute it once finished.
// @include     https://www.twitch.tv/*
// @include     https://twitch.tv/*
// @version     1.02
// @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;
                 };

// 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.");
    } else {
      console.log("Ad #" + _tmuteVars.adsDisplayed + " finished. Player unmuted.");
    }
  } else {
    console.log("No volume button found (class changed ?).");
  }
}

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