Browser extension to hide view-reducing screen junk from websites.

bannerbanner/src/ content_script.js
75 lines
1.8 KiB

  1. import browser from 'webextension-polyfill'
  2. function hideBanners() {
  3. const revertSteps = []
  4. /* Hide all elements with position:fixed */
  5. const fixedElements = Array.from(document.querySelectorAll('*'))
  6. .filter(el => getComputedStyle(el).position === 'fixed')
  7. fixedElements.forEach(el => {
  8. const originalDisplay = el.style.display
  9. const originalOpacity = el.style.opacity
  10. const originalTransition = el.style.transition
  11. el.style.transition = 'opacity 1s'
  12. el.style.opacity = '0'
  13. const hiderTimeoutId = window.setTimeout(() => {
  14. el.style.display = 'none'
  15. }, 1000)
  16. revertSteps.push(() => {
  17. window.clearTimeout(hiderTimeoutId)
  18. const reverterTimeoutId = window.setTimeout(() => {
  19. el.style.transition = originalTransition
  20. }, 1000)
  21. el.style.display = originalDisplay
  22. window.setTimeout(() => {
  23. el.style.opacity = originalOpacity
  24. })
  25. })
  26. })
  27. /* Stop position:sticky elements from sticking. */
  28. const stickyElements = Array.from(document.querySelectorAll('*'))
  29. .filter(el => getComputedStyle(el).position === 'sticky')
  30. stickyElements.forEach(el => {
  31. const originalPosition = el.style.position
  32. el.style.position = 'relative'
  33. revertSteps.push(() => {
  34. el.style.position = originalPosition
  35. })
  36. })
  37. const revert = () => revertSteps.forEach(step => step())
  38. return revert
  39. }
  40. function main() {
  41. let active = false
  42. let revert
  43. function onScroll() {
  44. let action
  45. if (window.scrollY > 20 && !active) {
  46. active = true
  47. action = () => { revert = hideBanners() }
  48. } else if (window.scrollY <= 0 && active) {
  49. active = false
  50. action = () => {
  51. revert && revert()
  52. revert = undefined
  53. }
  54. }
  55. if (action) {
  56. window.requestAnimationFrame(action)
  57. }
  58. }
  59. window.addEventListener('scroll', onScroll)
  60. }
  61. main()