From e8bcfc49cba7ff78f4dc1ef5b0532410b41632e6 Mon Sep 17 00:00:00 2001 From: mcpower Date: Sat, 21 Jan 2023 21:56:42 +1100 Subject: [PATCH] Delay getElement timeout until after rAF Under normal circumstances, this should be a negligible increase in the timeout, but if this tab is in the background, this rAF will delay until it has focus. Fixes #198. --- tweak-new-twitter.user.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tweak-new-twitter.user.js b/tweak-new-twitter.user.js index 2c2b7aa..d66e833 100644 --- a/tweak-new-twitter.user.js +++ b/tweak-new-twitter.user.js @@ -872,6 +872,7 @@ function getElement(selector, { let startTime = Date.now() let rafId let timeoutId + let stopped = false function stop($element, reason) { if ($element == null) { @@ -886,11 +887,21 @@ function getElement(selector, { if (timeoutId) { clearTimeout(timeoutId) } + stopped = true resolve($element) } if (timeout !== Infinity) { - timeoutId = setTimeout(stop, timeout, null, `${timeout}ms timeout`) + // Delay the timeout until after a rAF to avoid + // https://github.com/insin/tweak-new-twitter/issues/198. + // Under normal circumstances, this should be a negligible increase in the + // timeout, but if this tab is in the background, this rAF will delay + // until it has focus. + requestAnimationFrame(() => { + if (!stopped) { + timeoutId = setTimeout(stop, timeout, null, `${timeout}ms timeout`) + } + }) } function queryElement() { @@ -3370,4 +3381,4 @@ function configChanged(changes) { main() //#endregion -}() \ No newline at end of file +}()