From 7a111cae8599c42360fc414060a4d903d2bf6106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Shadowsun=E2=84=A2?= <12494967+CattoGamer@users.noreply.github.com> Date: Mon, 23 Dec 2024 02:14:41 +0100 Subject: [PATCH] Improve the behavior with ad recognition for YouTube --- .../mediaplayer/services/youtube/cl_init.lua | 115 ++++++++++-------- 1 file changed, 66 insertions(+), 49 deletions(-) diff --git a/workshop/lua/mediaplayer/services/youtube/cl_init.lua b/workshop/lua/mediaplayer/services/youtube/cl_init.lua index c8c72b6..973ff52 100644 --- a/workshop/lua/mediaplayer/services/youtube/cl_init.lua +++ b/workshop/lua/mediaplayer/services/youtube/cl_init.lua @@ -2,8 +2,18 @@ include "shared.lua" DEFINE_BASECLASS( "mp_service_browser" ) -local JS_Pause = "if(window.MediaPlayer) MediaPlayer.pause();" -local JS_Play = "if(window.MediaPlayer) MediaPlayer.play();" +local JS_Pause = [[ + if(window.MediaPlayer) { + MediaPlayer.pause(); + mp_paused = true + } +]] +local JS_Play = [[ + if(window.MediaPlayer) { + MediaPlayer.play(); + mp_paused = false + } +]] local JS_Volume = "if(window.MediaPlayer) MediaPlayer.volume = %s;" local JS_Seek = [[ if (window.MediaPlayer) { @@ -18,46 +28,24 @@ local JS_Seek = [[ ]] local JS_Interface = [[ + // YouTube Adblock (https://github.com/Vendicated/Vencord/blob/main/src/plugins/youtubeAdblock.desktop/adguard.js - #d199603) + const hiddenCSS=["#__ffYoutube1","#__ffYoutube2","#__ffYoutube3","#__ffYoutube4","#feed-pyv-container","#feedmodule-PRO","#homepage-chrome-side-promo","#merch-shelf","#offer-module",'#pla-shelf > ytd-pla-shelf-renderer[class="style-scope ytd-watch"]',"#pla-shelf","#premium-yva","#promo-info","#promo-list","#promotion-shelf","#related > ytd-watch-next-secondary-results-renderer > #items > ytd-compact-promoted-video-renderer.ytd-watch-next-secondary-results-renderer","#search-pva","#shelf-pyv-container","#video-masthead","#watch-branded-actions","#watch-buy-urls","#watch-channel-brand-div","#watch7-branded-banner","#YtKevlarVisibilityIdentifier","#YtSparklesVisibilityIdentifier",".carousel-offer-url-container",".companion-ad-container",".GoogleActiveViewElement",'.list-view[style="margin: 7px 0pt;"]',".promoted-sparkles-text-search-root-container",".promoted-videos",".searchView.list-view",".sparkles-light-cta",".watch-extra-info-column",".watch-extra-info-right",".ytd-carousel-ad-renderer",".ytd-compact-promoted-video-renderer",".ytd-companion-slot-renderer",".ytd-merch-shelf-renderer",".ytd-player-legacy-desktop-watch-ads-renderer",".ytd-promoted-sparkles-text-search-renderer",".ytd-promoted-video-renderer",".ytd-search-pyv-renderer",".ytd-video-masthead-ad-v3-renderer",".ytp-ad-action-interstitial-background-container",".ytp-ad-action-interstitial-slot",".ytp-ad-image-overlay",".ytp-ad-overlay-container",".ytp-ad-progress",".ytp-ad-progress-list",'[class*="ytd-display-ad-"]','[layout*="display-ad-"]','a[href^="http://www.youtube.com/cthru?"]','a[href^="https://www.youtube.com/cthru?"]',"ytd-action-companion-ad-renderer","ytd-banner-promo-renderer","ytd-compact-promoted-video-renderer","ytd-companion-slot-renderer","ytd-display-ad-renderer","ytd-promoted-sparkles-text-search-renderer","ytd-promoted-sparkles-web-renderer","ytd-search-pyv-renderer","ytd-single-option-survey-renderer","ytd-video-masthead-ad-advertiser-info-renderer","ytd-video-masthead-ad-v3-renderer","YTM-PROMOTED-VIDEO-RENDERER"],hideElements=()=>{if(!hiddenCSS)return;const e=hiddenCSS.join(", ")+" { display: none!important; }",r=document.createElement("style");r.textContent=e,document.head.appendChild(r)},observeDomChanges=e=>{new MutationObserver((r=>{e(r)})).observe(document.documentElement,{childList:!0,subtree:!0})},hideDynamicAds=()=>{const e=document.querySelectorAll("#contents > ytd-rich-item-renderer ytd-display-ad-renderer");0!==e.length&&e.forEach((e=>{if(e.parentNode&&e.parentNode.parentNode){const r=e.parentNode.parentNode;"ytd-rich-item-renderer"===r.localName&&(r.style.display="none")}}))},autoSkipAds=()=>{if(document.querySelector(".ad-showing")){const e=document.querySelector("video");e&&e.duration&&(e.currentTime=e.duration,setTimeout((()=>{const e=document.querySelector("button.ytp-ad-skip-button");e&&e.click()}),100))}},overrideObject=(e,r,t)=>{if(!e)return!1;let o=!1;for(const d in e)e.hasOwnProperty(d)&&d===r?(e[d]=t,o=!0):e.hasOwnProperty(d)&&"object"==typeof e[d]&&overrideObject(e[d],r,t)&&(o=!0);return o},jsonOverride=(e,r)=>{const t=JSON.parse;JSON.parse=(...o)=>{const d=t.apply(this,o);return overrideObject(d,e,r),d},Response.prototype.json=new Proxy(Response.prototype.json,{async apply(...t){const o=await Reflect.apply(...t);return overrideObject(o,e,r),o}})};jsonOverride("adPlacements",[]),jsonOverride("playerAds",[]),hideElements(),hideDynamicAds(),autoSkipAds(),observeDomChanges((()=>{hideDynamicAds(),autoSkipAds()})); + + var mp_paused = false var checkerInterval = setInterval(function() { - var player = document.getElementById("movie_player") || document.getElementsByClassName("html5-video-player")[0]; + var container = document.getElementById("movie_player"); + var video = container.querySelector(".html5-video-container video"); - if (!!player) { + if (!!video) { clearInterval(checkerInterval); - { // Native video controll - player.volume = 0; - player.currentTime = 0; - player.duration = player.getDuration(); - - Object.defineProperty(player, "volume", { - get() { - return player.getVolume(); - }, - set(volume) { - if (player.isMuted()) { - player.unMute(); - } - player.setVolume(volume * 100); - }, - }); - - Object.defineProperty(player, "currentTime", { - get() { - return Number(player.getCurrentTime()); - }, - set(time) { - player.seekTo(time, true); - }, - }); - } - { // Player resizer - document.body.appendChild(player); + document.body.appendChild(video); - player.style.backgroundColor = "#000"; - player.style.height = "100vh"; - player.style.left = '0px'; - player.style.width = '100%'; + video.style.backgroundColor = "#000"; + video.style.height = "100vh"; + video.style.left = '0px'; + video.style.width = '100%'; let countAmt = 0 let resizeTimer = setInterval(function() { @@ -66,7 +54,7 @@ local JS_Interface = [[ for (const elem of document.getElementsByTagName("ytd-app")) { elem.remove(); } for (const elem of document.getElementsByClassName("skeleton")) { elem.remove(); } - player.setInternalSize("100vw", "100vh"); + container.setInternalSize("100vw", "100vh"); document.body.style.overflow = "hidden"; countAmt++; @@ -77,13 +65,48 @@ local JS_Interface = [[ }, 10); } - window.MediaPlayer = player; + { // Popup Remover - https://github.com/TheRealJoelmatic/RemoveAdblockThing + setInterval(() => { + if (mp_paused) {return} + + const modalOverlay = document.querySelector("tp-yt-iron-overlay-backdrop"); + const popup = document.querySelector(".style-scope ytd-enforcement-message-view-model"); + const popupButton = document.getElementById("dismiss-button"); + + const bodyStyle = document.body.style; + bodyStyle.setProperty('overflow-y', 'auto', 'important'); + + if (modalOverlay) { + modalOverlay.removeAttribute("opened"); + modalOverlay.remove(); + } + + if (popup) { + + if (popupButton) popupButton.click(); + + popup.remove(); + video.play(); + + setTimeout(() => { + video.play(); + }, 500); + + } + // Check if the video is paused after removing the popup + if (!video.paused) return; + // UnPause The Video + video.play(); + + }, 1000); + } + + window.MediaPlayer = video; } }, 50); ]] local EMBED_URL = "https://www.youtube.com/watch?v=%s" -local ADBLOCK_JS = "" -- see end of file (https://github.com/Vendicated/Vencord/blob/main/src/plugins/youtubeAdblock.desktop/adguard.js - #d199603) --- -- Helper function for converting ISO 8601 time strings; this is the formatting @@ -136,10 +159,10 @@ end local function ParseElementAttribute( element, attribute ) if not element then return end -- Find the desired attribute - local output = string.match( element, attribute.."%s-=%s-%b\"\"" ) + local output = string.match( element, attribute .. "%s-=%s-%b\"\"" ) if not output then return end -- Remove the 'attribute=' part - output = string.gsub( output, attribute.."%s-=%s-", "" ) + output = string.gsub( output, attribute .. "%s-=%s-", "" ) -- Trim the quotes around the value string return string.sub( output, 2, -2 ) end @@ -221,8 +244,6 @@ function SERVICE:OnBrowserReady( browser ) browser:OpenURL( url ) browser.OnDocumentReady = function(pnl) - browser:RunJavascript(ADBLOCK_JS) - browser:QueueJavascript( JS_Interface ) end @@ -246,7 +267,7 @@ end function SERVICE:Sync() local seekTime = self:CurrentTime() - if self:IsTimed() and seekTime > 0 then + if IsValid(self.Browser) and self:IsTimed() and seekTime > 0 then self.Browser:RunJavascript(JS_Seek:format(seekTime)) end end @@ -284,8 +305,4 @@ do -- Metadata Prefech net.WriteString( self._metaTitle ) net.WriteUInt( self._metaDuration, 16 ) end -end - -ADBLOCK_JS = [[ -const hiddenCSS=["#__ffYoutube1","#__ffYoutube2","#__ffYoutube3","#__ffYoutube4","#feed-pyv-container","#feedmodule-PRO","#homepage-chrome-side-promo","#merch-shelf","#offer-module",'#pla-shelf > ytd-pla-shelf-renderer[class="style-scope ytd-watch"]',"#pla-shelf","#premium-yva","#promo-info","#promo-list","#promotion-shelf","#related > ytd-watch-next-secondary-results-renderer > #items > ytd-compact-promoted-video-renderer.ytd-watch-next-secondary-results-renderer","#search-pva","#shelf-pyv-container","#video-masthead","#watch-branded-actions","#watch-buy-urls","#watch-channel-brand-div","#watch7-branded-banner","#YtKevlarVisibilityIdentifier","#YtSparklesVisibilityIdentifier",".carousel-offer-url-container",".companion-ad-container",".GoogleActiveViewElement",'.list-view[style="margin: 7px 0pt;"]',".promoted-sparkles-text-search-root-container",".promoted-videos",".searchView.list-view",".sparkles-light-cta",".watch-extra-info-column",".watch-extra-info-right",".ytd-carousel-ad-renderer",".ytd-compact-promoted-video-renderer",".ytd-companion-slot-renderer",".ytd-merch-shelf-renderer",".ytd-player-legacy-desktop-watch-ads-renderer",".ytd-promoted-sparkles-text-search-renderer",".ytd-promoted-video-renderer",".ytd-search-pyv-renderer",".ytd-video-masthead-ad-v3-renderer",".ytp-ad-action-interstitial-background-container",".ytp-ad-action-interstitial-slot",".ytp-ad-image-overlay",".ytp-ad-overlay-container",".ytp-ad-progress",".ytp-ad-progress-list",'[class*="ytd-display-ad-"]','[layout*="display-ad-"]','a[href^="http://www.youtube.com/cthru?"]','a[href^="https://www.youtube.com/cthru?"]',"ytd-action-companion-ad-renderer","ytd-banner-promo-renderer","ytd-compact-promoted-video-renderer","ytd-companion-slot-renderer","ytd-display-ad-renderer","ytd-promoted-sparkles-text-search-renderer","ytd-promoted-sparkles-web-renderer","ytd-search-pyv-renderer","ytd-single-option-survey-renderer","ytd-video-masthead-ad-advertiser-info-renderer","ytd-video-masthead-ad-v3-renderer","YTM-PROMOTED-VIDEO-RENDERER"],hideElements=()=>{if(!hiddenCSS)return;const e=hiddenCSS.join(", ")+" { display: none!important; }",r=document.createElement("style");r.textContent=e,document.head.appendChild(r)},observeDomChanges=e=>{new MutationObserver((r=>{e(r)})).observe(document.documentElement,{childList:!0,subtree:!0})},hideDynamicAds=()=>{const e=document.querySelectorAll("#contents > ytd-rich-item-renderer ytd-display-ad-renderer");0!==e.length&&e.forEach((e=>{if(e.parentNode&&e.parentNode.parentNode){const r=e.parentNode.parentNode;"ytd-rich-item-renderer"===r.localName&&(r.style.display="none")}}))},autoSkipAds=()=>{if(document.querySelector(".ad-showing")){const e=document.querySelector("video");e&&e.duration&&(e.currentTime=e.duration,setTimeout((()=>{const e=document.querySelector("button.ytp-ad-skip-button");e&&e.click()}),100))}},overrideObject=(e,r,t)=>{if(!e)return!1;let o=!1;for(const d in e)e.hasOwnProperty(d)&&d===r?(e[d]=t,o=!0):e.hasOwnProperty(d)&&"object"==typeof e[d]&&overrideObject(e[d],r,t)&&(o=!0);return o},jsonOverride=(e,r)=>{const t=JSON.parse;JSON.parse=(...o)=>{const d=t.apply(this,o);return overrideObject(d,e,r),d},Response.prototype.json=new Proxy(Response.prototype.json,{async apply(...t){const o=await Reflect.apply(...t);return overrideObject(o,e,r),o}})};jsonOverride("adPlacements",[]),jsonOverride("playerAds",[]),hideElements(),hideDynamicAds(),autoSkipAds(),observeDomChanges((()=>{hideDynamicAds(),autoSkipAds()})); -]] \ No newline at end of file +end \ No newline at end of file