2023-08-27 11:43:10 +00:00
|
|
|
// ==UserScript==
|
|
|
|
// @name Hide sticky elements
|
|
|
|
// @namespace blankie-scripts
|
|
|
|
// @match http*://*/*
|
|
|
|
// @grant GM_registerMenuCommand
|
|
|
|
// @grant GM_unregisterMenuCommand
|
2023-09-10 00:37:32 +00:00
|
|
|
// @grant GM_addStyle
|
2023-09-28 10:11:27 +00:00
|
|
|
// @version 1.1.1
|
2023-08-27 11:43:10 +00:00
|
|
|
// @author blankie
|
|
|
|
// @description Adds a button to the monkey menu to hide sticky elements
|
|
|
|
// @inject-into content
|
|
|
|
// ==/UserScript==
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2023-09-10 00:37:32 +00:00
|
|
|
let stickyElements = new Set();
|
|
|
|
let style = null;
|
|
|
|
let observer = new MutationObserver(function(mutations) {
|
|
|
|
for (let mutation of mutations) {
|
|
|
|
handleOneNode(mutation.target);
|
|
|
|
for (let node of mutation.addedNodes) {
|
|
|
|
handleNode(node);
|
|
|
|
}
|
|
|
|
for (let node of mutation.removedNodes) {
|
|
|
|
handleNode(node, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-27 11:43:10 +00:00
|
|
|
let hideStickyElements = true;
|
|
|
|
let menuId;
|
|
|
|
|
2023-09-10 00:37:32 +00:00
|
|
|
function handleOneNode(node, removed = false) {
|
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let computedStyle = getComputedStyle(node);
|
2023-09-28 10:11:27 +00:00
|
|
|
let isSticky = /^(sticky|fixed|absolute)$/.test(computedStyle.position);
|
2023-09-10 00:37:32 +00:00
|
|
|
if (!stickyElements.has(node) && isSticky && !removed) {
|
|
|
|
node.classList.add("hseSticky");
|
|
|
|
stickyElements.add(node);
|
|
|
|
} else if (stickyElements.has(node) && (!isSticky || removed)) {
|
|
|
|
node.classList.remove("hseSticky");
|
|
|
|
stickyElements.delete(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleNode(node, removed = false) {
|
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-27 11:43:10 +00:00
|
|
|
|
2023-09-10 00:37:32 +00:00
|
|
|
handleOneNode(node, removed);
|
|
|
|
for (let element of node.querySelectorAll("*")) {
|
|
|
|
handleOneNode(element, removed);
|
2023-08-27 11:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-10 00:37:32 +00:00
|
|
|
function hideAllStickyElements() {
|
|
|
|
style = GM_addStyle(".hseSticky { display: none !important; }");
|
|
|
|
handleNode(document.body);
|
|
|
|
observer.observe(document.body, {subtree: true, childList: true, attributes: true});
|
|
|
|
}
|
|
|
|
|
2023-08-27 11:43:10 +00:00
|
|
|
function unhideAllStickyElements() {
|
2023-09-10 00:37:32 +00:00
|
|
|
observer.disconnect();
|
|
|
|
style.remove();
|
|
|
|
style = null;
|
|
|
|
|
|
|
|
for (let element of stickyElements) {
|
|
|
|
element.classList.remove("hseSticky");
|
2023-08-27 11:43:10 +00:00
|
|
|
}
|
2023-09-10 00:37:32 +00:00
|
|
|
stickyElements.clear();
|
2023-08-27 11:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|