Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Refresh Twitch when ad appears
// ==UserScript==
// @name twitchAdRefresh
// @namespace https://www.twitch.tv/
// @version 2.0
// @description Bezos is already too rich
// @author SimpleHacker
// @match https://www.twitch.tv/*
// @grant none
// @homepageURL https://gist.github.com/simple-hacker/ddd81964b3e8bca47e0aead5ad19a707
// ==/UserScript==
(function() {
'use strict';
const findVideoPlayer = function() {
const videoPlayer = document.querySelector('[data-a-target="video-player"]');
if (videoPlayer) {
attachMutationObserver(videoPlayer);
}
}
const attachMutationObserver = function(videoPlayer) {
const dblclick = new MouseEvent('dblclick', {
bubbles: true,
cancelable: true,
view: window
});
let options = {
childList: true,
subtree: true
};
const adObserver = new MutationObserver(function(mutations) {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
let ad = node.querySelector('[data-test-selector="ad-banner-default-text"]');
if (ad) {
let resetButton = document.querySelector('[data-a-target="ffz-player-reset-button"]');
let videoPlayerElement = document.querySelector('video');
let currentVolume = videoPlayerElement.volume;
if (resetButton) {
resetButton.dispatchEvent(dblclick);
setTimeout(() => {
videoPlayerElement.volume = currentVolume;
}, 2000);
} else {
window.location.reload();
}
}
}
});
});
});
adObserver.observe(videoPlayer, options);
}
window.onload = function() {
findVideoPlayer();
var pushState = history.pushState;
history.pushState = function () {
pushState.apply(history, arguments);
findVideoPlayer();
};
}
})();
@Wilkolicious
Copy link

Wilkolicious commented Nov 1, 2020

Nice work, thanks for this.

Perhaps add the following TM meta bits:

// @updateURL    https://gist.githubusercontent.com/simple-hacker/ddd81964b3e8bca47e0aead5ad19a707/raw/refreshTwitchAd.js
// @downloadURL  https://gist.githubusercontent.com/simple-hacker/ddd81964b3e8bca47e0aead5ad19a707/raw/refreshTwitchAd.js

Not sure if TM supports it, but viewing 'raw' did not prompt for TM installation, and have removed the revision hash scope from the URL.

Loading

@Onurtag
Copy link

Onurtag commented Nov 3, 2020

The raw button works if the file has a .user.js extension. If you just rename the file we should be good.

Loading

@GabRider
Copy link

GabRider commented Nov 4, 2020

thanks for this script.

Unfortunately there is a bug with the sound. For fix this removes those lines :

line 44: let videoPlayerElement = document.querySelector('video');

line 45: .let currentVolume = videoPlayerElement.volume;

line 48-50 : setTimeout(() => { videoPlayerElement.volume = currentVolume; }, 2000);

I think you recover the sound of the ads and not the sound of the stream

Loading

@Wilkolicious
Copy link

Wilkolicious commented Nov 4, 2020

I'm currently maintaining a script based on OP's that should fix any problems (loading or volume persistence) here: https://github.com/Wilkolicious/twitchAdSkip

If OP's script doesn't work for you, then give mine a quick whirl.

Loading

@lewiskj
Copy link

lewiskj commented Nov 5, 2020

Works exactly as intended, but glitches the volume bar (which can be fixed by manually readjusting it.)

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment