From 6eefa4d2fa02a51a34b61a5dea45f6b42d637060 Mon Sep 17 00:00:00 2001 From: pixeltris <6952411+pixeltris@users.noreply.github.com> Date: Sun, 20 Jul 2025 21:02:57 +0100 Subject: [PATCH] Fix muted streams pausing when switching tabs on chrome #336 --- vaft/vaft-ublock-origin.js | 23 ++++++++++++++--- vaft/vaft.user.js | 25 +++++++++++++++---- .../video-swap-new-ublock-origin.js | 23 ++++++++++++++--- video-swap-new/video-swap-new.user.js | 25 +++++++++++++++---- 4 files changed, 78 insertions(+), 18 deletions(-) diff --git a/vaft/vaft-ublock-origin.js b/vaft/vaft-ublock-origin.js index 66b7b98..f9e76ec 100644 --- a/vaft/vaft-ublock-origin.js +++ b/vaft/vaft-ublock-origin.js @@ -1,7 +1,7 @@ twitch-videoad.js text/javascript (function() { if ( /(^|\.)twitch\.tv$/.test(document.location.hostname) === false ) { return; } - var ourTwitchAdSolutionsVersion = 7;// Only bump this when there's a breaking change to Twitch, the script, or there's a conflict with an unmaintained extension which uses this script + var ourTwitchAdSolutionsVersion = 8;// Used to prevent conflicts with outdated versions of the scripts if (typeof unsafeWindow === 'undefined') { unsafeWindow = window; } @@ -1018,6 +1018,7 @@ twitch-videoad.js text/javascript } }); }catch{} + let hidden = document.__lookupGetter__('hidden'); try { Object.defineProperty(document, 'hidden', { get() { @@ -1030,9 +1031,23 @@ twitch-videoad.js text/javascript e.stopPropagation(); e.stopImmediatePropagation(); }; - document.addEventListener('visibilitychange', block, true); - document.addEventListener('webkitvisibilitychange', block, true); - document.addEventListener('mozvisibilitychange', block, true); + let wasVideoPlaying = true; + var visibilityChange = e => { + if (typeof chrome !== 'undefined') { + const videos = document.getElementsByTagName('video'); + if (videos.length > 0) { + if (hidden.apply(document) === true) { + wasVideoPlaying = !videos[0].paused && !videos[0].ended; + } else if (wasVideoPlaying && !videos[0].ended) { + videos[0].play(); + } + } + } + block(e); + }; + document.addEventListener('visibilitychange', visibilityChange, true); + document.addEventListener('webkitvisibilitychange', visibilityChange, true); + document.addEventListener('mozvisibilitychange', visibilityChange, true); document.addEventListener('hasFocus', block, true); try { if (/Firefox/.test(navigator.userAgent)) { diff --git a/vaft/vaft.user.js b/vaft/vaft.user.js index f5c58a7..b03a9d1 100644 --- a/vaft/vaft.user.js +++ b/vaft/vaft.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name TwitchAdSolutions (vaft) // @namespace https://github.com/pixeltris/TwitchAdSolutions -// @version 22.0.0 +// @version 23.0.0 // @description Multiple solutions for blocking Twitch ads (vaft) // @updateURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js // @downloadURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js @@ -14,7 +14,7 @@ // ==/UserScript== (function() { 'use strict'; - var ourTwitchAdSolutionsVersion = 7;// Only bump this when there's a breaking change to Twitch, the script, or there's a conflict with an unmaintained extension which uses this script + var ourTwitchAdSolutionsVersion = 8;// Used to prevent conflicts with outdated versions of the scripts if (typeof unsafeWindow === 'undefined') { unsafeWindow = window; } @@ -1031,6 +1031,7 @@ } }); }catch{} + let hidden = document.__lookupGetter__('hidden'); try { Object.defineProperty(document, 'hidden', { get() { @@ -1043,9 +1044,23 @@ e.stopPropagation(); e.stopImmediatePropagation(); }; - document.addEventListener('visibilitychange', block, true); - document.addEventListener('webkitvisibilitychange', block, true); - document.addEventListener('mozvisibilitychange', block, true); + let wasVideoPlaying = true; + var visibilityChange = e => { + if (typeof chrome !== 'undefined') { + const videos = document.getElementsByTagName('video'); + if (videos.length > 0) { + if (hidden.apply(document) === true) { + wasVideoPlaying = !videos[0].paused && !videos[0].ended; + } else if (wasVideoPlaying && !videos[0].ended) { + videos[0].play(); + } + } + } + block(e); + }; + document.addEventListener('visibilitychange', visibilityChange, true); + document.addEventListener('webkitvisibilitychange', visibilityChange, true); + document.addEventListener('mozvisibilitychange', visibilityChange, true); document.addEventListener('hasFocus', block, true); try { if (/Firefox/.test(navigator.userAgent)) { diff --git a/video-swap-new/video-swap-new-ublock-origin.js b/video-swap-new/video-swap-new-ublock-origin.js index df3d3b1..56baad4 100644 --- a/video-swap-new/video-swap-new-ublock-origin.js +++ b/video-swap-new/video-swap-new-ublock-origin.js @@ -1,7 +1,7 @@ twitch-videoad.js text/javascript (function() { if ( /(^|\.)twitch\.tv$/.test(document.location.hostname) === false ) { return; } - var ourTwitchAdSolutionsVersion = 7;// Only bump this when there's a breaking change to Twitch, the script, or there's a conflict with an unmaintained extension which uses this script + var ourTwitchAdSolutionsVersion = 8;// Used to prevent conflicts with outdated versions of the scripts if (typeof unsafeWindow === 'undefined') { unsafeWindow = window; } @@ -789,6 +789,7 @@ twitch-videoad.js text/javascript } }); }catch{} + let hidden = document.__lookupGetter__('hidden'); try { Object.defineProperty(document, 'hidden', { get() { @@ -801,9 +802,23 @@ twitch-videoad.js text/javascript e.stopPropagation(); e.stopImmediatePropagation(); }; - document.addEventListener('visibilitychange', block, true); - document.addEventListener('webkitvisibilitychange', block, true); - document.addEventListener('mozvisibilitychange', block, true); + let wasVideoPlaying = true; + var visibilityChange = e => { + if (typeof chrome !== 'undefined') { + const videos = document.getElementsByTagName('video'); + if (videos.length > 0) { + if (hidden.apply(document) === true) { + wasVideoPlaying = !videos[0].paused && !videos[0].ended; + } else if (wasVideoPlaying && !videos[0].ended) { + videos[0].play(); + } + } + } + block(e); + }; + document.addEventListener('visibilitychange', visibilityChange, true); + document.addEventListener('webkitvisibilitychange', visibilityChange, true); + document.addEventListener('mozvisibilitychange', visibilityChange, true); document.addEventListener('hasFocus', block, true); try { if (/Firefox/.test(navigator.userAgent)) { diff --git a/video-swap-new/video-swap-new.user.js b/video-swap-new/video-swap-new.user.js index 5456356..d9085c2 100644 --- a/video-swap-new/video-swap-new.user.js +++ b/video-swap-new/video-swap-new.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name TwitchAdSolutions (video-swap-new) // @namespace https://github.com/pixeltris/TwitchAdSolutions -// @version 1.40 +// @version 1.41 // @updateURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/video-swap-new/video-swap-new.user.js // @downloadURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/video-swap-new/video-swap-new.user.js // @description Multiple solutions for blocking Twitch ads (video-swap-new) @@ -14,7 +14,7 @@ // ==/UserScript== (function() { 'use strict'; - var ourTwitchAdSolutionsVersion = 7;// Only bump this when there's a breaking change to Twitch, the script, or there's a conflict with an unmaintained extension which uses this script + var ourTwitchAdSolutionsVersion = 8;// Used to prevent conflicts with outdated versions of the scripts if (typeof unsafeWindow === 'undefined') { unsafeWindow = window; } @@ -802,6 +802,7 @@ } }); }catch{} + let hidden = document.__lookupGetter__('hidden'); try { Object.defineProperty(document, 'hidden', { get() { @@ -814,9 +815,23 @@ e.stopPropagation(); e.stopImmediatePropagation(); }; - document.addEventListener('visibilitychange', block, true); - document.addEventListener('webkitvisibilitychange', block, true); - document.addEventListener('mozvisibilitychange', block, true); + let wasVideoPlaying = true; + var visibilityChange = e => { + if (typeof chrome !== 'undefined') { + const videos = document.getElementsByTagName('video'); + if (videos.length > 0) { + if (hidden.apply(document) === true) { + wasVideoPlaying = !videos[0].paused && !videos[0].ended; + } else if (wasVideoPlaying && !videos[0].ended) { + videos[0].play(); + } + } + } + block(e); + }; + document.addEventListener('visibilitychange', visibilityChange, true); + document.addEventListener('webkitvisibilitychange', visibilityChange, true); + document.addEventListener('mozvisibilitychange', visibilityChange, true); document.addEventListener('hasFocus', block, true); try { if (/Firefox/.test(navigator.userAgent)) {