Unfixes sticky elements such as navigation bar. Default keymap: ctrl+alt+u
当前为
// ==UserScript==
// @name Unfix
// @namespace com.gmail.fujifruity.greasemonkey
// @version 1.0
// @description Unfixes sticky elements such as navigation bar. Default keymap: ctrl+alt+u
// @author fujifruity
// @match *://*/*
// @grant none
// ==/UserScript==
(() => {
let log = (...msg) => console.log("Unfix:", ...msg)
// Gets the css of the element
function cssOf(elem) { return document.defaultView.getComputedStyle(elem, '') }
function isFixed(elem) { return ['fixed', 'sticky'].includes(cssOf(elem).position) }
// Unfixes the element
function unfix(elem) {
if (isFixed(elem)) {
elem.style.setProperty('position', 'unset', 'important')
log('unfixed:', elem)
}
}
// Keeps all elements unfixed
function unfixForever() {
// Unfix all fixed elements
let allElems = Array.from(document.body.getElementsByTagName("*"))
allElems.forEach(unfix)
// Start observing elements get fixed
let observer = new window.MutationObserver((mutationRecords) => {
mutationRecords.forEach((mutationRecord) => {
unfix(mutationRecord.target)
})
})
let config = { attributes: true, subtree: true }
observer.observe(document.body, config)
log('observing...')
}
// Set a shortcut
window.addEventListener('keydown', (event) => {
if (event.key == 'u' && event.ctrlKey == true && event.altKey == true) {
unfixForever()
log('keydown')
}
})
log('set shortcut')
})();