From 6a7aaf8394893d7d8ef633c0a5453bbc66b5a5ba Mon Sep 17 00:00:00 2001 From: Ven0m0 <82972344+Ven0m0@users.noreply.github.com> Date: Sat, 2 May 2026 17:07:12 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Improve=20MutationObserver=20addedN?= =?UTF-8?q?odes=20performance=20in=20yt-pro?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- userscripts/src/yt-pro.user.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/userscripts/src/yt-pro.user.js b/userscripts/src/yt-pro.user.js index 0a14d17..20ae349 100644 --- a/userscripts/src/yt-pro.user.js +++ b/userscripts/src/yt-pro.user.js @@ -406,23 +406,28 @@ obs.observe(e); }; const lazy = () => document.querySelectorAll(sel).forEach(processNode); + let moTimeout; const mo = new MutationObserver((mutations) => { - for (const m of mutations) { - for (const node of m.addedNodes) { - if (node.nodeType === 1) { - const n = node.nodeName; - if ( - (n === "YTD-RICH-ITEM-RENDERER" || n === "YTD-COMPACT-VIDEO-RENDERER" || n === "YTD-THUMBNAIL") && - !node.dataset.lazyOpt - ) { - processNode(node); - } - if (node.querySelectorAll) { - const els = node.querySelectorAll(sel); - for (let i = 0; i < els.length; i++) processNode(els[i]); - } + let hasAdded = false; + for (let i = 0; i < mutations.length; i++) { + const added = mutations[i].addedNodes; + for (let j = 0; j < added.length; j++) { + if (added[j].nodeType === 1) { + hasAdded = true; + break; } } + if (hasAdded) break; + } + + if (hasAdded) { + if (moTimeout) clearTimeout(moTimeout); + moTimeout = setTimeout(() => { + const els = document.querySelectorAll(sel); + for (let i = 0; i < els.length; i++) { + processNode(els[i]); + } + }, 100); } }); mo.observe(document.body || document.documentElement, { childList: true, subtree: true });