diff --git a/plugins/LastSeen/renderer.js b/plugins/LastSeen/renderer.js index 7a7dc1d..41d8318 100644 --- a/plugins/LastSeen/renderer.js +++ b/plugins/LastSeen/renderer.js @@ -418,8 +418,11 @@ function updBlk(b, id) { const data = lines(id); if (!data) return b.remove(); + const signature = JSON.stringify(data); + if (b.dataset.slickLs === id && b.dataset.slickLsSignature === signature) return; b.textContent = ''; b.dataset.slickLs = id; + b.dataset.slickLsSignature = signature; for (const ln of data) { const line = document.createElement('span'); line.className = 'slick-ls-line'; @@ -571,7 +574,13 @@ if (!document.body) return setTimeout(boot, 200); patchWS(); pall(); - new MutationObserver(sched).observe(document.body, { subtree: true, childList: true }); + new MutationObserver((mutations) => { + const external = mutations.some((mutation) => { + const target = mutation.target; + return target.nodeType !== Node.ELEMENT_NODE || !target.closest('[data-slick-ls]'); + }); + if (external) sched(); + }).observe(document.body, { subtree: true, childList: true }); addEventListener('slick:plugin-settings', sched); addEventListener('storage', (e) => { if (e.key === 'slick:lastseen:cache') { diff --git a/plugins/UserPronouns/renderer.js b/plugins/UserPronouns/renderer.js index 9a188a7..feecffb 100644 --- a/plugins/UserPronouns/renderer.js +++ b/plugins/UserPronouns/renderer.js @@ -3,6 +3,7 @@ if (window.__slickPronouns) return; const ID_RE = /^[UW][A-Z0-9]{6,}$/; + const ROW_SEL = '.c-message_kit__message, .c-message, [data-qa="message_container"], [role="listitem"]'; let cache = loadCache(); let dirty = false; @@ -215,7 +216,7 @@ } function paint(ts) { - const row = ts.closest('.c-message_kit__message, .c-message, [data-qa="message_container"], [role="listitem"]'); + const row = ts.closest(ROW_SEL); const sender = row && row.querySelector( @@ -251,10 +252,23 @@ } } + function paintWithin(root) { + const timestamps = []; + if (root.nodeType === Node.ELEMENT_NODE && root.matches('.c-timestamp')) timestamps.push(root); + if (root.querySelectorAll) timestamps.push(...root.querySelectorAll('.c-timestamp')); + const rows = new Set(); + timestamps.forEach((timestamp) => { + const row = timestamp.closest(ROW_SEL); + if (!row || rows.has(row)) return; + rows.add(row); + paint(timestamp); + }); + if (dirty) saveCache(); + } + function paintAll() { syncColor(); - document.querySelectorAll('.c-timestamp').forEach(paint); - if (dirty) saveCache(); + paintWithin(document); } window.__slickPronouns = { @@ -263,11 +277,21 @@ }; let t = null; - const obs = new MutationObserver(() => { + const pendingRoots = new Set(); + const obs = new MutationObserver((mutations) => { + mutations.forEach((mutation) => { + mutation.addedNodes.forEach((node) => { + if (node.nodeType === Node.ELEMENT_NODE) pendingRoots.add(node); + }); + }); + if (!pendingRoots.size) return; if (t) return; t = setTimeout(() => { t = null; - paintAll(); + const roots = [...pendingRoots]; + pendingRoots.clear(); + syncColor(); + roots.forEach(paintWithin); }, 200); }); function boot() {