From 447776924fdb61c636320bf22ad3bf25adfb4fb1 Mon Sep 17 00:00:00 2001 From: pixeltris <6952411+pixeltris@users.noreply.github.com> Date: Sun, 13 Jun 2021 13:26:14 +0100 Subject: [PATCH] Use pause/play instead of seekTo due to time backshift --- base/base.user.js | 49 ++++++++++++++++++-- notify-reload/notify-reload-ublock-origin.js | 47 ++++++++++++++++++- notify-reload/notify-reload.user.js | 49 ++++++++++++++++++-- notify-strip/notify-strip-ublock-origin.js | 47 ++++++++++++++++++- notify-strip/notify-strip.user.js | 49 ++++++++++++++++++-- 5 files changed, 228 insertions(+), 13 deletions(-) diff --git a/base/base.user.js b/base/base.user.js index 6b1c85b..2efc15c 100644 --- a/base/base.user.js +++ b/base/base.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name TwitchAdSolutions // @namespace https://github.com/pixeltris/TwitchAdSolutions -// @version 1.7 +// @version 1.8 // @description Multiple solutions for blocking Twitch ads // @author pixeltris // @match *://*.twitch.tv/* @@ -265,7 +265,7 @@ console.log('Fetching backup m3u8 failed'); console.log(err); } - // Backups failed. Return nothing (this will likely result in spam or player error 2000?). + // Backups failed. Return nothing and reload the player (reload required as an empty result will terminate the stream). console.log('Ad blocking failed. Stream might break.'); postMessage({key:'UboReloadPlayer'}); streamInfo.BackupFailed = false; @@ -273,7 +273,7 @@ return ''; } if (streamInfo.HadAds) { - postMessage({key:'UboSeekPlayer'}); + postMessage({key:'UboPauseResumePlayer'}); streamInfo.HadAds = false; } postMessage({key:'UboHideAdBanner'}); @@ -604,4 +604,47 @@ } window.reloadTwitchPlayer = reloadTwitchPlayer; hookFetch(); + function onContentLoaded() { + // This stops Twitch from pausing the player when in another tab and an ad shows. + // Taken from https://github.com/saucettv/VideoAdBlockForTwitch/blob/cefce9d2b565769c77e3666ac8234c3acfe20d83/chrome/content.js#L30 + Object.defineProperty(document, 'visibilityState', { + get() { + return 'visible'; + } + }); + Object.defineProperty(document, 'hidden', { + get() { + return false; + } + }); + const block = e => { + e.preventDefault(); + e.stopPropagation(); + e.stopImmediatePropagation(); + }; + document.addEventListener('visibilitychange', block, true); + document.addEventListener('webkitvisibilitychange', block, true); + document.addEventListener('mozvisibilitychange', block, true); + document.addEventListener('hasFocus', block, true); + if (/Firefox/.test(navigator.userAgent)) { + Object.defineProperty(document, 'mozHidden', { + get() { + return false; + } + }); + } else { + Object.defineProperty(document, 'webkitHidden', { + get() { + return false; + } + }); + } + } + if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") { + onContentLoaded(); + } else { + window.addEventListener("DOMContentLoaded", function() { + onContentLoaded(); + }); + } })(); \ No newline at end of file diff --git a/notify-reload/notify-reload-ublock-origin.js b/notify-reload/notify-reload-ublock-origin.js index e0d3127..3cd26a0 100644 --- a/notify-reload/notify-reload-ublock-origin.js +++ b/notify-reload/notify-reload-ublock-origin.js @@ -256,7 +256,7 @@ twitch-videoad.js application/javascript console.log('Fetching backup m3u8 failed'); console.log(err); } - // Backups failed. Return nothing (this will likely result in spam or player error 2000?). + // Backups failed. Return nothing and reload the player (reload required as an empty result will terminate the stream). console.log('Ad blocking failed. Stream might break.'); postMessage({key:'UboReloadPlayer'}); streamInfo.BackupFailed = false; @@ -264,7 +264,7 @@ twitch-videoad.js application/javascript return ''; } if (streamInfo.HadAds) { - postMessage({key:'UboSeekPlayer'}); + postMessage({key:'UboPauseResumePlayer'}); streamInfo.HadAds = false; } postMessage({key:'UboHideAdBanner'}); @@ -595,4 +595,47 @@ twitch-videoad.js application/javascript } window.reloadTwitchPlayer = reloadTwitchPlayer; hookFetch(); + function onContentLoaded() { + // This stops Twitch from pausing the player when in another tab and an ad shows. + // Taken from https://github.com/saucettv/VideoAdBlockForTwitch/blob/cefce9d2b565769c77e3666ac8234c3acfe20d83/chrome/content.js#L30 + Object.defineProperty(document, 'visibilityState', { + get() { + return 'visible'; + } + }); + Object.defineProperty(document, 'hidden', { + get() { + return false; + } + }); + const block = e => { + e.preventDefault(); + e.stopPropagation(); + e.stopImmediatePropagation(); + }; + document.addEventListener('visibilitychange', block, true); + document.addEventListener('webkitvisibilitychange', block, true); + document.addEventListener('mozvisibilitychange', block, true); + document.addEventListener('hasFocus', block, true); + if (/Firefox/.test(navigator.userAgent)) { + Object.defineProperty(document, 'mozHidden', { + get() { + return false; + } + }); + } else { + Object.defineProperty(document, 'webkitHidden', { + get() { + return false; + } + }); + } + } + if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") { + onContentLoaded(); + } else { + window.addEventListener("DOMContentLoaded", function() { + onContentLoaded(); + }); + } })(); diff --git a/notify-reload/notify-reload.user.js b/notify-reload/notify-reload.user.js index 191882a..bc699e7 100644 --- a/notify-reload/notify-reload.user.js +++ b/notify-reload/notify-reload.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name TwitchAdSolutions (notify-reload) // @namespace https://github.com/pixeltris/TwitchAdSolutions -// @version 1.7 +// @version 1.8 // @updateURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/notify-reload/notify-reload.user.js // @downloadURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/notify-reload/notify-reload.user.js // @description Multiple solutions for blocking Twitch ads (notify-reload) @@ -267,7 +267,7 @@ console.log('Fetching backup m3u8 failed'); console.log(err); } - // Backups failed. Return nothing (this will likely result in spam or player error 2000?). + // Backups failed. Return nothing and reload the player (reload required as an empty result will terminate the stream). console.log('Ad blocking failed. Stream might break.'); postMessage({key:'UboReloadPlayer'}); streamInfo.BackupFailed = false; @@ -275,7 +275,7 @@ return ''; } if (streamInfo.HadAds) { - postMessage({key:'UboSeekPlayer'}); + postMessage({key:'UboPauseResumePlayer'}); streamInfo.HadAds = false; } postMessage({key:'UboHideAdBanner'}); @@ -606,4 +606,47 @@ } window.reloadTwitchPlayer = reloadTwitchPlayer; hookFetch(); + function onContentLoaded() { + // This stops Twitch from pausing the player when in another tab and an ad shows. + // Taken from https://github.com/saucettv/VideoAdBlockForTwitch/blob/cefce9d2b565769c77e3666ac8234c3acfe20d83/chrome/content.js#L30 + Object.defineProperty(document, 'visibilityState', { + get() { + return 'visible'; + } + }); + Object.defineProperty(document, 'hidden', { + get() { + return false; + } + }); + const block = e => { + e.preventDefault(); + e.stopPropagation(); + e.stopImmediatePropagation(); + }; + document.addEventListener('visibilitychange', block, true); + document.addEventListener('webkitvisibilitychange', block, true); + document.addEventListener('mozvisibilitychange', block, true); + document.addEventListener('hasFocus', block, true); + if (/Firefox/.test(navigator.userAgent)) { + Object.defineProperty(document, 'mozHidden', { + get() { + return false; + } + }); + } else { + Object.defineProperty(document, 'webkitHidden', { + get() { + return false; + } + }); + } + } + if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") { + onContentLoaded(); + } else { + window.addEventListener("DOMContentLoaded", function() { + onContentLoaded(); + }); + } })(); diff --git a/notify-strip/notify-strip-ublock-origin.js b/notify-strip/notify-strip-ublock-origin.js index 97ab4a6..856acd9 100644 --- a/notify-strip/notify-strip-ublock-origin.js +++ b/notify-strip/notify-strip-ublock-origin.js @@ -256,7 +256,7 @@ twitch-videoad.js application/javascript console.log('Fetching backup m3u8 failed'); console.log(err); } - // Backups failed. Return nothing (this will likely result in spam or player error 2000?). + // Backups failed. Return nothing and reload the player (reload required as an empty result will terminate the stream). console.log('Ad blocking failed. Stream might break.'); postMessage({key:'UboReloadPlayer'}); streamInfo.BackupFailed = false; @@ -264,7 +264,7 @@ twitch-videoad.js application/javascript return ''; } if (streamInfo.HadAds) { - postMessage({key:'UboSeekPlayer'}); + postMessage({key:'UboPauseResumePlayer'}); streamInfo.HadAds = false; } postMessage({key:'UboHideAdBanner'}); @@ -595,4 +595,47 @@ twitch-videoad.js application/javascript } window.reloadTwitchPlayer = reloadTwitchPlayer; hookFetch(); + function onContentLoaded() { + // This stops Twitch from pausing the player when in another tab and an ad shows. + // Taken from https://github.com/saucettv/VideoAdBlockForTwitch/blob/cefce9d2b565769c77e3666ac8234c3acfe20d83/chrome/content.js#L30 + Object.defineProperty(document, 'visibilityState', { + get() { + return 'visible'; + } + }); + Object.defineProperty(document, 'hidden', { + get() { + return false; + } + }); + const block = e => { + e.preventDefault(); + e.stopPropagation(); + e.stopImmediatePropagation(); + }; + document.addEventListener('visibilitychange', block, true); + document.addEventListener('webkitvisibilitychange', block, true); + document.addEventListener('mozvisibilitychange', block, true); + document.addEventListener('hasFocus', block, true); + if (/Firefox/.test(navigator.userAgent)) { + Object.defineProperty(document, 'mozHidden', { + get() { + return false; + } + }); + } else { + Object.defineProperty(document, 'webkitHidden', { + get() { + return false; + } + }); + } + } + if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") { + onContentLoaded(); + } else { + window.addEventListener("DOMContentLoaded", function() { + onContentLoaded(); + }); + } })(); diff --git a/notify-strip/notify-strip.user.js b/notify-strip/notify-strip.user.js index 3f9f280..abb9c0e 100644 --- a/notify-strip/notify-strip.user.js +++ b/notify-strip/notify-strip.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name TwitchAdSolutions (notify-strip) // @namespace https://github.com/pixeltris/TwitchAdSolutions -// @version 1.7 +// @version 1.8 // @updateURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/notify-strip/notify-strip.user.js // @downloadURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/notify-strip/notify-strip.user.js // @description Multiple solutions for blocking Twitch ads (notify-strip) @@ -267,7 +267,7 @@ console.log('Fetching backup m3u8 failed'); console.log(err); } - // Backups failed. Return nothing (this will likely result in spam or player error 2000?). + // Backups failed. Return nothing and reload the player (reload required as an empty result will terminate the stream). console.log('Ad blocking failed. Stream might break.'); postMessage({key:'UboReloadPlayer'}); streamInfo.BackupFailed = false; @@ -275,7 +275,7 @@ return ''; } if (streamInfo.HadAds) { - postMessage({key:'UboSeekPlayer'}); + postMessage({key:'UboPauseResumePlayer'}); streamInfo.HadAds = false; } postMessage({key:'UboHideAdBanner'}); @@ -606,4 +606,47 @@ } window.reloadTwitchPlayer = reloadTwitchPlayer; hookFetch(); + function onContentLoaded() { + // This stops Twitch from pausing the player when in another tab and an ad shows. + // Taken from https://github.com/saucettv/VideoAdBlockForTwitch/blob/cefce9d2b565769c77e3666ac8234c3acfe20d83/chrome/content.js#L30 + Object.defineProperty(document, 'visibilityState', { + get() { + return 'visible'; + } + }); + Object.defineProperty(document, 'hidden', { + get() { + return false; + } + }); + const block = e => { + e.preventDefault(); + e.stopPropagation(); + e.stopImmediatePropagation(); + }; + document.addEventListener('visibilitychange', block, true); + document.addEventListener('webkitvisibilitychange', block, true); + document.addEventListener('mozvisibilitychange', block, true); + document.addEventListener('hasFocus', block, true); + if (/Firefox/.test(navigator.userAgent)) { + Object.defineProperty(document, 'mozHidden', { + get() { + return false; + } + }); + } else { + Object.defineProperty(document, 'webkitHidden', { + get() { + return false; + } + }); + } + } + if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") { + onContentLoaded(); + } else { + window.addEventListener("DOMContentLoaded", function() { + onContentLoaded(); + }); + } })();