From ba7aab5e5d5382217517ebe761fe945e72b549bd Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Mon, 19 Aug 2024 17:35:03 +0300 Subject: [PATCH] refactor mutation observers --- unpacked/pageAccess.js | 51 ++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/unpacked/pageAccess.js b/unpacked/pageAccess.js index 7172935..ed2d2e9 100644 --- a/unpacked/pageAccess.js +++ b/unpacked/pageAccess.js @@ -836,13 +836,26 @@ if (ytvs.$('#movie_player:not(.unstarted-mode) video')) { if (moviePlayer) { documentObserver.observe(moviePlayer, { attributes: true }); } else { - setTimeout(() => - documentObserver.observe( - ytvs.$('ytd-page-manager'), - { childList: true }, - ytvs.$('ytd-page-manager') ? 0 : 200, - ), - ); + const pageManager = ytvs.$('ytd-page-manager'); + if (pageManager) { + documentObserver.observe(pageManager, { + childList: true, + subtree: true, + }); + } else { + new MutationObserver((_, observer) => { + if (ytvs.$('ytd-page-manager')) { + observer.disconnect(); + documentObserver.observe(ytvs.$('ytd-page-manager'), { + childList: true, + subtree: true, + }); + } + }).observe(document.documentElement, { + childList: true, + subtree: true, + }); + } } } @@ -868,12 +881,22 @@ if (!ytvs.isMusic) { ); } }); - setTimeout(() => { - shortsListener.observe( - ytvs.$('ytd-page-manager'), - { childList: true }, - ytvs.$('ytd-page-manager') ? 0 : 200, - ); - }); + const pageManager = ytvs.$('ytd-page-manager'); + if (pageManager) { + shortsListener.observe(pageManager, { childList: true }); + } else { + new MutationObserver((_, observer) => { + if (ytvs.$('ytd-page-manager')) { + observer.disconnect(); + shortsListener.observe(ytvs.$('ytd-page-manager'), { + childList: true, + subtree: true, + }); + } + }).observe(document.documentElement, { + childList: true, + subtree: true, + }); + } } }