Unfix

Unfixes sticky elements such as navigation bar. Default keymap: ctrl+alt+u

目前為 2020-12-20 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Unfix
  3. // @namespace com.gmail.fujifruity.greasemonkey
  4. // @version 1.0
  5. // @description Unfixes sticky elements such as navigation bar. Default keymap: ctrl+alt+u
  6. // @author fujifruity
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11.  
  12. (() => {
  13. let log = (...msg) => console.log("Unfix:", ...msg)
  14.  
  15. // Gets the css of the element
  16. function cssOf(elem) { return document.defaultView.getComputedStyle(elem, '') }
  17. function isFixed(elem) { return ['fixed', 'sticky'].includes(cssOf(elem).position) }
  18.  
  19. // Unfixes the element
  20. function unfix(elem) {
  21. if (isFixed(elem)) {
  22. elem.style.setProperty('position', 'unset', 'important')
  23. log('unfixed:', elem)
  24. }
  25. }
  26.  
  27. // Keeps all elements unfixed
  28. function unfixForever() {
  29. // Unfix all fixed elements
  30. let allElems = Array.from(document.body.getElementsByTagName("*"))
  31. allElems.forEach(unfix)
  32.  
  33. // Start observing elements get fixed
  34. let observer = new window.MutationObserver((mutationRecords) => {
  35. mutationRecords.forEach((mutationRecord) => {
  36. unfix(mutationRecord.target)
  37. })
  38. })
  39. let config = { attributes: true, subtree: true }
  40. observer.observe(document.body, config)
  41. log('observing...')
  42. }
  43.  
  44. // Set a shortcut
  45. window.addEventListener('keydown', (event) => {
  46. if (event.key == 'u' && event.ctrlKey == true && event.altKey == true) {
  47. unfixForever()
  48. log('keydown')
  49. }
  50. })
  51. log('set shortcut')
  52.  
  53. })();