From 661a2687bc8782553a87ceaa58a84967b6077507 Mon Sep 17 00:00:00 2001 From: Gerben Date: Thu, 15 Feb 2018 23:46:51 +0100 Subject: [PATCH] Listen to scroll events --- src/content_script.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/content_script.js b/src/content_script.js index 6f3efdc..5eb6202 100644 --- a/src/content_script.js +++ b/src/content_script.js @@ -47,20 +47,28 @@ function hideBanners() { } -function startCheckingLoop() { +function main() { + let active = false let revert - function checkScroll() { - if (window.scrollY > 20 && !revert) { - revert = hideBanners() - } else if (window.scrollY <= 0 && revert) { - revert() - revert = undefined + function onScroll() { + let action + if (window.scrollY > 20 && !active) { + active = true + action = () => { revert = hideBanners() } + } else if (window.scrollY <= 0 && active) { + active = false + action = () => { + revert && revert() + revert = undefined + } + } + if (action) { + window.requestAnimationFrame(action) } - window.requestAnimationFrame(checkScroll); } - window.requestAnimationFrame(checkScroll); + window.addEventListener('scroll', onScroll) } -startCheckingLoop(); +main()