Twitch Ad blocker

Blocks All twitch ads 100% of the time! Never view another ad!

当前为 2025-06-02 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Twitch Ad blocker
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Blocks All twitch ads 100% of the time! Never view another ad!
// @author       You
// @match        https://www.twitch.tv/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const CHECK_INTERVAL = 3000; // ms
    const AUTO_REFRESH = false;  // Set to true to auto-refresh stream on ad

    function log(msg) {
        console.log(`[TwitchAdBypass] ${msg}`);
    }

    function detectAd() {
        const adContainer = document.querySelector('.ad-banner__container,.tw-video-ad,.player-ad-overlay');

        // Twitch also adds a "purple screen" placeholder with this class sometimes
        const purpleScreen = document.querySelector('div[data-test-selector="video-player-ad-label"]');

        return adContainer || purpleScreen;
    }

    function mutePlayer() {
        const video = document.querySelector('video');
        if (video && !video.muted) {
            video.muted = true;
            log('Muted stream (ad detected)');
        }
    }

    function unmutePlayer() {
        const video = document.querySelector('video');
        if (video && video.muted) {
            video.muted = false;
            log('Unmuted stream (no ad)');
        }
    }

    function refreshPlayer() {
        const video = document.querySelector('video');
        if (video) {
            log('Refreshing stream (ad detected)');
            location.reload(); // reloads page — safer than trying to reload just the player
        }
    }

    setInterval(() => {
        const adIsPlaying = detectAd();

        if (adIsPlaying) {
            if (AUTO_REFRESH) {
                refreshPlayer();
            } else {
                mutePlayer();
            }
        } else {
            unmutePlayer();
        }
    }, CHECK_INTERVAL);
})();