Last active
October 5, 2021 08:20
-
-
Save pauldambra/3a59a7c355bbfb794cda006399173c7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Fade Twitter | |
// @namespace http://tampermonkey.net/ | |
// @version 0.3 | |
// @description fades twitter over time and by scrolling to protect me from myself | |
// @author You | |
// @match https://twitter.com/* | |
// @grant none | |
// ==/UserScript== | |
const isoDay = () => (new Date()).toISOString().split('T')[0] | |
const storageKey = `fade-twitter-by-${isoDay()}` | |
const fadeAmount = () => parseFloat(localStorage[storageKey] || '1.0', 10) | |
let lastScroll = 0 | |
let debounce = false | |
let isActive = true | |
document.addEventListener("visibilitychange", function() { | |
isActive = document.visibilityState || document.hasFocus() | |
}); | |
const fade = (reason = 'on timer') => { | |
if (isActive === 'hidden') { | |
return | |
} | |
const f = fadeAmount() | |
document.body.style.opacity = f | |
localStorage[storageKey] = ''+ (f * 0.99) | |
console.log({thisFade: reason, fadeTo: f}) | |
} | |
const onScroll = (e) => { | |
if (!debounce) { | |
window.requestAnimationFrame(() => { | |
const nextScroll = window.scrollY | |
const scrolledBy = lastScroll - nextScroll | |
if (Math.abs(scrolledBy) > 1000) { | |
lastScroll = nextScroll | |
fade('on scrolling') | |
} | |
debounce = false | |
}); | |
debounce = true | |
} | |
} | |
document.addEventListener('scroll', onScroll) | |
window.setInterval(fade, 30000) | |
fade() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment