|
- import browser from 'webextension-polyfill'
- import throttle from 'lodash/throttle'
-
- import { onEnabledChange, isEnabled } from './perOrigin'
-
-
- function hideBanners() {
- const revertSteps = []
-
- /* Hide all elements with position:fixed */
- const fixedElements = Array.from(document.querySelectorAll('*'))
- .filter(el => (
- getComputedStyle(el).position === 'fixed'
- && el.style.opacity !== '0' // ignore if we already hid this one
- ))
-
- fixedElements.forEach(el => {
- const originalVisibility = el.style.visibility
- const originalOpacity = el.style.opacity
- const originalTransition = el.style.transition
- el.style.transition = 'opacity 1s'
- el.style.opacity = '0'
-
- const hiderTimeoutId = window.setTimeout(() => {
- el.style.visibility = 'hidden'
- }, 1000)
-
- revertSteps.push(() => {
- window.clearTimeout(hiderTimeoutId)
- const reverterTimeoutId = window.setTimeout(() => {
- el.style.transition = originalTransition
- }, 1000)
- el.style.visibility = originalVisibility
- el.style.opacity = originalOpacity
- })
- })
-
- /* Stop position:sticky elements from sticking. */
- const stickyElements = Array.from(document.querySelectorAll('*'))
- .filter(el => getComputedStyle(el).position === 'sticky')
-
- stickyElements.forEach(el => {
- const originalPosition = el.style.position
- el.style.position = 'relative'
- revertSteps.push(() => {
- el.style.position = originalPosition
- })
- })
-
- return revertSteps
- }
-
- function main() {
- let revertSteps = []
-
- // XXX We read the location only once, but it may be changed by page scripts.
- // Fortunately we only need the origin, which should remain the same.
- const url = window.location.href
-
- async function run() {
- const enabled = await isEnabled(url)
- if (enabled && window.scrollY > 20) {
- // Hide everything that looks annoying. Remember the steps to revert this.
- revertSteps = hideBanners().concat(revertSteps)
- } else if (!enabled || window.scrollY <= 0) {
- // Unhide everything we have hidden.
- revertSteps.forEach(step => step())
- revertSteps = []
- }
- }
-
- window.addEventListener('scroll', throttle(run, 300))
- onEnabledChange(url, run)
- }
-
- main()
|