Last active
December 31, 2020 22:40
-
-
Save gartnera/94a4d39d104cc3a44276d296aae909c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Mute preroll ads on twitch | |
// @namespace http://agartner.com | |
// @version 0.3 | |
// @author Alex Gartner | |
// @match https://www.twitch.tv/* | |
// ==/UserScript== | |
let interval; | |
let didMute = false; | |
function set_player_blackout(shouldBlackout) { | |
const player = document.querySelector('div.video-player > div > div > div > div:nth-child(1) > div'); | |
const color = shouldBlackout ? 'black': ''; | |
player.style.backgroundColor = color; | |
} | |
function set_mute(shouldMute) { | |
const muteButton = document.querySelector('button[data-a-target="player-mute-unmute-button"]'); | |
const isCurrentlyMuted = muteButton.getAttribute('aria-label').includes('Unmute'); | |
if (shouldMute) { | |
if (!isCurrentlyMuted) { | |
muteButton.click() | |
didMute = true; | |
set_player_blackout(true); | |
} | |
// handle player already muted | |
if (!didMute) { | |
didMute = true; | |
set_player_blackout(true); | |
} | |
} else { | |
if (isCurrentlyMuted && didMute) { | |
muteButton.click() | |
didMute = false; | |
set_player_blackout(false); | |
} | |
} | |
} | |
function loop() { | |
const playerText = document.getElementsByClassName('video-player')[0].innerText; | |
const adStatus = playerText.includes('ad break'); | |
set_mute(adStatus) | |
} | |
interval = setInterval(loop, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment