From fb227efe4bd98267ca6836017f515df06dab324b Mon Sep 17 00:00:00 2001 From: Gerben Date: Sat, 10 Mar 2018 23:09:40 +0100 Subject: [PATCH] Use _.throttle like normal people would. --- package.json | 1 + src/content_script.js | 9 +++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 36f9f4d..d3c0328 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "web-ext": "^1.9.1" }, "dependencies": { + "lodash": "^4.17.5", "webextension-polyfill": "^0.1.1" }, "browserify": { diff --git a/src/content_script.js b/src/content_script.js index bdb878e..0c9c85b 100644 --- a/src/content_script.js +++ b/src/content_script.js @@ -1,4 +1,5 @@ import browser from 'webextension-polyfill' +import throttle from 'lodash/throttle' function hideBanners() { const revertSteps = [] @@ -50,14 +51,10 @@ function hideBanners() { function main() { let state = 'off' let revertSteps = [] - let lastRun function onScroll(event) { let action - if (window.scrollY > 20 && - (state === 'off' || (state === 'on' && event.timeStamp - lastRun > 1000)) - ) { - lastRun = event.timeStamp + if (window.scrollY > 20 && (state === 'off' || state === 'on')) { state = 'starting' action = () => { revertSteps = hideBanners().concat(revertSteps) @@ -76,7 +73,7 @@ function main() { } } - window.addEventListener('scroll', onScroll) + window.addEventListener('scroll', throttle(onScroll, 1000)) } main()