Unfix

Unfixes all fixed elements such as navigation bar and keeps them unfixed. Keybind: ctrl+alt+u

当前为 2021-11-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Unfix
  3. // @namespace com.gmail.fujifruity.greasemonkey
  4. // @version 1.1
  5. // @description Unfixes all fixed elements such as navigation bar and keeps them unfixed. Keybind: ctrl+alt+u
  6. // @author fujifruity
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. {
  12. const cssOf = e => document.defaultView.getComputedStyle(e, '')
  13.  
  14. const isFixed = e => ['fixed', 'sticky'].includes(cssOf(e).position)
  15.  
  16. const unfix = e => {
  17. if (!isFixed(e)) return
  18. if (e.id == 'fujifruity-toc') return
  19. e.style.setProperty('position', 'unset', 'important')
  20. console.log('unfixed: ', e)
  21. }
  22.  
  23. // Keeps all elements unfixed
  24. const unfixForever = () => {
  25. // Unfix all fixed elements
  26. const allElems = Array.from(document.body.getElementsByTagName("*"))
  27. allElems.forEach(unfix)
  28.  
  29. // Start observing elements get fixed
  30. const observer = new window.MutationObserver((mutationRecords) => {
  31. mutationRecords.forEach((mutationRecord) => {
  32. unfix(mutationRecord.target)
  33. })
  34. })
  35. const config = { attributes: true, subtree: true }
  36. observer.observe(document.body, config)
  37. }
  38.  
  39. // Set keybinds
  40. window.addEventListener('keydown', event => {
  41. if (event.key == 'u' && event.ctrlKey == true && event.altKey == true) {
  42. unfixForever()
  43. }
  44. })
  45. }