From f4c0d42cd87f9c27a099fbe6a52c388318eea4d2 Mon Sep 17 00:00:00 2001 From: goodtube4u Date: Wed, 22 Oct 2025 15:11:28 +1000 Subject: [PATCH] REVERT --- goodtube.js | 223 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 134 insertions(+), 89 deletions(-) diff --git a/goodtube.js b/goodtube.js index 92bff08..6bd381d 100644 --- a/goodtube.js +++ b/goodtube.js @@ -83,12 +83,37 @@ } } - // Add a CSS class to show or hide elements + // Add CSS classes to show or hide elements / the Youtube player function goodTube_helper_showHide_init() { let style = document.createElement('style'); style.textContent = ` .goodTube_hidden { - display: none !important; + position: fixed !important; + top: -9999px !important; + left: -9999px !important; + transform: scale(0) !important; + pointer-events: none !important; + } + + .goodTube_hiddenPlayer { + position: relative; + overflow: hidden; + z-index: 1; + } + + .goodTube_hiddenPlayer::before { + content: ''; + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + background: #ffffff; + z-index: 998; + } + + html[dark] .goodTube_hiddenPlayer::before { + background: #0f0f0f; } `; document.head.appendChild(style); @@ -108,6 +133,15 @@ } } + // Show the Youtube player + function goodTube_helper_showYoutubePlayer(element) { + let wrappingElement = element.closest('.goodTube_hiddenPlayer'); + + if (wrappingElement) { + wrappingElement.classList.remove('goodTube_hiddenPlayer'); + } + } + // Check if we're watching a video function goodTube_helper_watchingVideo() { // If the URL contains "/watch/" or "/watch?" or "/live/", we're viewing a video @@ -334,7 +368,7 @@ // Hide the main Youtube player cssOutput += ` - body:not(.goodTube_fallback) ytd-player { + body:not('.goodTube_fallback) ytd-player { visibility: hidden !important; } `; @@ -547,6 +581,7 @@ } // Turn off autoplay + let goodTube_youtube_turnOffAutoplay_timeout = setTimeout(() => {}, 0); function goodTube_youtube_turnOffAutoplay() { // If we've already turned off autoplay, just return if (goodTube_turnedOffAutoplay) { @@ -566,6 +601,14 @@ // Set a variable if autoplay has been turned off goodTube_turnedOffAutoplay = true; } + + // Keep doing this, Youtube is causing autoplay issues lately...it doesn't want to stay off? + + // Clear timeout first to solve memory leak issues + clearTimeout(goodTube_youtube_turnOffAutoplay_timeout); + + // Run actions again in 100ms to loop this function + goodTube_youtube_turnOffAutoplay_timeout = setTimeout(goodTube_youtube_turnOffAutoplay, 100); } // Remove the "are you still watching" popup @@ -695,44 +738,57 @@ goodTube_playerWrapper = document.querySelector('#goodTube_playerWrapper'); goodTube_player = goodTube_playerWrapper.querySelector('iframe'); - // Run the actions every 100ms - setInterval(goodTube_actions, 100); + // Setup player dynamic positioning and sizing + goodTube_player_positionAndSize(); + + // Run the actions + goodTube_actions(); } // Position and size the player + let goodTube_player_positionAndSize_timeout = setTimeout(() => {}, 0); let goodTube_clearedPlayer = false; function goodTube_player_positionAndSize() { - // If the "hide and mute ads" fallback is inactive - if (goodTube_fallback) { - if (!goodTube_clearedPlayer) { - // Hide and clear the embedded player - goodTube_player_clear(true); - goodTube_clearedPlayer = true; + // If we're viewing a video + if (goodTube_helper_watchingVideo()) { + // If the "hide and mute ads" fallback is inactive + if (goodTube_fallback) { + if (!goodTube_clearedPlayer) { + // Hide and clear the embedded player + goodTube_player_clear(true); + goodTube_clearedPlayer = true; + } + } + // Otherwise, the "hide and mute ads" fallback is inactive + else { + goodTube_clearedPlayer = false; + + // Show the GoodTube player + goodTube_helper_showElement(goodTube_playerWrapper); + + // Get the Youtube player + let youtubePlayer = document.querySelector('#ytd-player, .player-size'); + + // If we found the Youtube player + if (youtubePlayer && youtubePlayer.offsetHeight > 0) { + // Make our custom player match the position of the Youtube player + // Note: Our custom player uses "position: absolute" so take into account the window scroll + let rect = youtubePlayer.getBoundingClientRect(); + goodTube_playerWrapper.style.top = (rect.top + window.scrollY) + 'px'; + goodTube_playerWrapper.style.left = (rect.left + window.scrollX) + 'px'; + + // Make our custom player match the size of the Youtube player + goodTube_playerWrapper.style.width = youtubePlayer.offsetWidth + 'px'; + goodTube_playerWrapper.style.height = youtubePlayer.offsetHeight + 'px'; + } } } - // Otherwise, the "hide and mute ads" fallback is inactive - else { - goodTube_clearedPlayer = false; - // Show the GoodTube player - goodTube_helper_showElement(goodTube_playerWrapper); + // Clear timeout first to solve memory leak issues + clearTimeout(goodTube_player_positionAndSize_timeout); - // Get the Youtube player - let youtubePlayer = document.querySelector('#ytd-player, .player-size'); - - // If we found the Youtube player - if (youtubePlayer && youtubePlayer.offsetHeight > 0) { - // Make our custom player match the position of the Youtube player - // Note: Our custom player uses "position: absolute" so take into account the window scroll - let rect = youtubePlayer.getBoundingClientRect(); - goodTube_playerWrapper.style.top = (rect.top + window.scrollY) + 'px'; - goodTube_playerWrapper.style.left = (rect.left + window.scrollX) + 'px'; - - // Make our custom player match the size of the Youtube player - goodTube_playerWrapper.style.width = youtubePlayer.offsetWidth + 'px'; - goodTube_playerWrapper.style.height = youtubePlayer.offsetHeight + 'px'; - } - } + // Create a new timeout + goodTube_player_positionAndSize_timeout = setTimeout(goodTube_player_positionAndSize, 100); } // Populate the playlist info @@ -837,6 +893,10 @@ goodTube_player_syncStartingTime(); } + // Set the Youtube player to auto quality + goodTube_player_setQualitySucceeded = false; + goodTube_player_setAutoQuality(); + // Show the player goodTube_helper_showElement(goodTube_playerWrapper); @@ -844,6 +904,15 @@ goodTube_player_play(); } + // Set the Youtube player quality + let goodTube_player_setQualitySucceeded = false; + let goodTube_player_setQuality_timeout = setTimeout(() => {}, 0); + function goodTube_player_setAutoQuality(quality = 'highest') { + // DISABLE FOR NOW THIS IS CAUSING ISSUES + return; + + } + // Sync the starting time let goodTube_player_syncStartingTime_timeout = setTimeout(() => {}, 0); function goodTube_player_syncStartingTime() { @@ -1274,7 +1343,6 @@ ------------------------------------------------------------------------------------------ */ // Init let goodTube_initiated = false; - let goodTube_init_timeout = setTimeout(() => {}, 0); function goodTube_init() { // Listen for messages from the iframes window.addEventListener('message', goodTube_receiveMessage); @@ -1289,19 +1357,6 @@ if (document.readyState === 'interactive' || document.readyState === 'complete') { goodTube_init_domReady(); } - - // And try this to check if the DOM is ready, seems to be the only reliable method in all browsers (which is insane, I know...thanks Safari) - if (!document.body || !document.head) { - // Clear timeout first to solve memory leak issues - clearTimeout(goodTube_init_timeout); - - // Create a new timeout - goodTube_init_timeout = setTimeout(goodTube_init, 1); - } - // Otherwise, the DOM is ready - else { - goodTube_init_domReady(); - } } // Init when DOM is ready @@ -1315,7 +1370,7 @@ // Check the tab focus state goodTube_checkTabFocus(); - // Add a CSS class to show or hide elements + // Add CSS classes to hide elements (without Youtube knowing) goodTube_helper_showHide_init(); // Hide page elements @@ -1346,7 +1401,7 @@ } // Make sure the DOM is ready, if not retry (this ensures that the message will fire eventually) - if (!document.body || !document.head) { + if ((document.readyState !== 'interactive' && document.readyState !== 'complete') || !document.body || !document.head) { // Clear timeout first to solve memory leak issues clearTimeout(goodTube_receiveMessage_timeout); @@ -1504,8 +1559,8 @@ goodTube_fallback = false; // Remove the class from the - if (document.body && document.body.classList.contains('goodTube_fallback')) { - document.body.classList.remove('goodTube_fallback'); + if (document.body && !document.body.classList.contains('goodTube_fallback')) { + document.body.classList.add('goodTube_fallback'); } // If we're in fullscreen already @@ -1528,6 +1583,7 @@ } // Actions + let goodTube_actions_timeout = setTimeout(() => {}, 0); function goodTube_actions() { // Get the previous and current URL @@ -1586,16 +1642,16 @@ // Remove the "are you still watching" popup goodTube_youtube_removeAreYouStillWatchingPopup(); - - // Position and size the player - goodTube_player_positionAndSize(); - - // Check to enable or disable the "hide and mute ads" fallback overlay - goodTube_hideAndMuteAdsFallback_check(); } // Hide shorts (real time) goodTube_youtube_hideShortsRealTime(); + + // Clear timeout first to solve memory leak issues + clearTimeout(goodTube_actions_timeout); + + // Run actions again in 100ms to loop this function + goodTube_actions_timeout = setTimeout(goodTube_actions, 100); } // Init menu @@ -2405,6 +2461,9 @@ style.textContent = cssOutput; document.head.appendChild(style); + // Check to enable or disable the overlay + goodTube_hideAndMuteAdsFallback_check(); + // Disable some shortcuts while the overlay is enabled function disableShortcuts(event) { // Make sure we're watching a video and the overlay state is disabled @@ -2465,6 +2524,7 @@ } // Check to enable or disable the overlay + let goodTube_hideAndMuteAdsFallback_check_timeout = setTimeout(() => {}, 0); function goodTube_hideAndMuteAdsFallback_check() { // If the "hide and mute ads" fallback is active AND we're viewing a video if (goodTube_fallback && goodTube_helper_watchingVideo()) { @@ -2483,6 +2543,12 @@ else { goodTube_hideAndMuteAds_state = ''; } + + // Clear timeout first to solve memory leak issues + clearTimeout(goodTube_hideAndMuteAdsFallback_check_timeout); + + // Run actions again in 1ms to loop this function + goodTube_hideAndMuteAdsFallback_check_timeout = setTimeout(goodTube_hideAndMuteAdsFallback_check, 100); } // Enable the the overlay @@ -2736,7 +2802,6 @@ ------------------------------------------------------------------------------------------ */ // Init let goodTube_iframe_initiated = false; - let goodTube_iframe_init_timeout = setTimeout(() => {}, 0); function goodTube_iframe_init() { // Listen for messages from the parent window window.addEventListener('message', goodTube_iframe_receiveMessage); @@ -2748,19 +2813,6 @@ if (document.readyState === 'interactive' || document.readyState === 'complete') { goodTube_iframe_init_domReady(); } - - // And try this to check if the DOM is ready, seems to be the only reliable method in all browsers (which is insane, I know...thanks Safari) - if (!document.body || !document.head) { - // Clear timeout first to solve memory leak issues - clearTimeout(goodTube_iframe_init_timeout); - - // Create a new timeout - goodTube_iframe_init_timeout = setTimeout(goodTube_iframe_init, 1); - } - // Otherwise, the DOM is ready - else { - goodTube_iframe_init_domReady(); - } } // Init when DOM is ready @@ -2781,7 +2833,7 @@ clearTimeout(goodTube_iframe_init_domReady_timeout); // Create a new timeout - goodTube_iframe_init_domReady_timeout = setTimeout(goodTube_iframe_init_domReady, 1); + goodTube_iframe_init_domReady_timeout = setTimeout(goodTube_iframe_init_domReady, 100); return; } @@ -2816,14 +2868,15 @@ // Restore playback speed, and update it if it changes goodTube_iframe_playbackSpeed(); - // Run the iframe actions every 100ms - setInterval(goodTube_iframe_actions, 100); + // Run the iframe actions + goodTube_iframe_actions(); // Let the parent frame know it's loaded window.top.postMessage('goodTube_playerIframe_loaded', '*'); } // Actions + let goodTube_iframe_actions_timeout = setTimeout(() => {}, 0); function goodTube_iframe_actions() { // Check to see if the "hide and mute ads" fallback should be active goodTube_iframe_hideMuteAdsFallback(); @@ -2844,6 +2897,12 @@ // Sync the aspect ratio goodTube_iframe_syncAspectRatio(); + + // Clear timeout first to solve memory leak issues + clearTimeout(goodTube_iframe_actions_timeout); + + // Create a new timeout + goodTube_iframe_actions_timeout = setTimeout(goodTube_iframe_actions, 100); } // Check to see if the "hide and mute ads" fallback should be active @@ -3786,7 +3845,7 @@ } // Make sure the DOM is ready, if not retry (this ensures that the message will fire eventually) - if (!document.body || !document.head) { + if ((document.readyState !== 'interactive' && document.readyState !== 'complete') || !document.body || !document.head) { // Clear timeout first to solve memory leak issues clearTimeout(goodTube_iframe_receiveMessage_timeout); @@ -4223,7 +4282,6 @@ ------------------------------------------------------------------------------------------ */ // Init let goodTube_proxyIframe_initiated = false; - let goodTube_proxyIframe_init_timeout = setTimeout(() => {}, 0); function goodTube_proxyIframe_init() { // Listen for messages from the parent window window.addEventListener('message', goodTube_proxyIframe_receiveMessage); @@ -4235,19 +4293,6 @@ if (document.readyState === 'interactive' || document.readyState === 'complete') { goodTube_proxyIframe_init_domReady(); } - - // And try this to check if the DOM is ready, seems to be the only reliable method in all browsers (which is insane, I know...thanks Safari) - if (!document.body || !document.head) { - // Clear timeout first to solve memory leak issues - clearTimeout(goodTube_proxyIframe_init_timeout); - - // Create a new timeout - goodTube_proxyIframe_init_timeout = setTimeout(goodTube_proxyIframe_init, 1); - } - // Otherwise, the DOM is ready - else { - goodTube_proxyIframe_init_domReady(); - } } // Init when DOM is ready @@ -4326,7 +4371,7 @@ } // Make sure the DOM is ready, if not retry (this ensures that the message will fire eventually) - if (!document.body || !document.head) { + if (document.readyState !== 'interactive' && document.readyState !== 'complete') { // Clear timeout first to solve memory leak issues clearTimeout(goodTube_proxyIframe_receiveMessage_timeout);