userscripts/Hide sticky elements.user.js

54 lines
1.7 KiB
JavaScript

// ==UserScript==
// @name Hide sticky elements
// @namespace blankie-scripts
// @match http*://*/*
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @version 1.0.0
// @author blankie
// @description Adds a button to the monkey menu to hide sticky elements
// @inject-into content
// ==/UserScript==
"use strict";
let stickyElements = [];
let hideStickyElements = true;
let menuId;
function hideAllStickyElements() {
for (let element of document.body.querySelectorAll("*")) {
let style = getComputedStyle(element);
if (style.position !== "sticky" && style.position !== "fixed") {
continue;
}
let originalDisplayValue = element.style.getPropertyValue("display");
let originalDisplayPriority = element.style.getPropertyPriority("display");
element.style.setProperty("display", "none", "important");
stickyElements.push([element, originalDisplayValue, originalDisplayPriority]);
}
}
function unhideAllStickyElements() {
for (let [element, originalDisplayValue, originalDisplayPriority] of stickyElements) {
element.style.setProperty("display", originalDisplayValue, originalDisplayPriority);
}
stickyElements = [];
}
function registerMenu() {
if (menuId) {
GM_unregisterMenuCommand(menuId);
}
let caption = hideStickyElements ? "Hide sticky elements" : "Unhide sticky elements";
menuId = GM_registerMenuCommand(caption, function() {
(hideStickyElements ? hideAllStickyElements : unhideAllStickyElements)();
hideStickyElements = !hideStickyElements;
registerMenu();
});
}
registerMenu();