Twitch - Mute ads

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

目前為 2018-08-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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.0
// @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('twitch-stitched-ad');
  //if (advert.length == 0) advert = document.getElementsByClassName('player-ad-notice'); // fallback test
  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 === false)
    {
      _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);