Update Hide sticky elements to 1.1.0
- Handle sticky elements added after the they're hidden, for example: https://web.archive.org/web/20220909140854/https://www.scientificamerican.com/article/we-asked-gpt-3-to-write-an-academic-paper-about-itself-mdash-then-we-tried-to-get-it-published/
This commit is contained in:
parent
505e5fd682
commit
a2b8370309
|
@ -4,7 +4,8 @@
|
||||||
// @match http*://*/*
|
// @match http*://*/*
|
||||||
// @grant GM_registerMenuCommand
|
// @grant GM_registerMenuCommand
|
||||||
// @grant GM_unregisterMenuCommand
|
// @grant GM_unregisterMenuCommand
|
||||||
// @version 1.0.0
|
// @grant GM_addStyle
|
||||||
|
// @version 1.1.0
|
||||||
// @author blankie
|
// @author blankie
|
||||||
// @description Adds a button to the monkey menu to hide sticky elements
|
// @description Adds a button to the monkey menu to hide sticky elements
|
||||||
// @inject-into content
|
// @inject-into content
|
||||||
|
@ -12,29 +13,65 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
let stickyElements = [];
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let hideStickyElements = true;
|
let hideStickyElements = true;
|
||||||
let menuId;
|
let menuId;
|
||||||
|
|
||||||
function hideAllStickyElements() {
|
function handleOneNode(node, removed = false) {
|
||||||
for (let element of document.body.querySelectorAll("*")) {
|
if (node.nodeType !== Node.ELEMENT_NODE) {
|
||||||
let style = getComputedStyle(element);
|
return;
|
||||||
if (style.position !== "sticky" && style.position !== "fixed") {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let originalDisplayValue = element.style.getPropertyValue("display");
|
let computedStyle = getComputedStyle(node);
|
||||||
let originalDisplayPriority = element.style.getPropertyPriority("display");
|
let isSticky = computedStyle.position === "sticky" || computedStyle.position === "fixed";
|
||||||
element.style.setProperty("display", "none", "important");
|
if (!stickyElements.has(node) && isSticky && !removed) {
|
||||||
stickyElements.push([element, originalDisplayValue, originalDisplayPriority]);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOneNode(node, removed);
|
||||||
|
for (let element of node.querySelectorAll("*")) {
|
||||||
|
handleOneNode(element, removed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function hideAllStickyElements() {
|
||||||
|
style = GM_addStyle(".hseSticky { display: none !important; }");
|
||||||
|
handleNode(document.body);
|
||||||
|
observer.observe(document.body, {subtree: true, childList: true, attributes: true});
|
||||||
|
}
|
||||||
|
|
||||||
function unhideAllStickyElements() {
|
function unhideAllStickyElements() {
|
||||||
for (let [element, originalDisplayValue, originalDisplayPriority] of stickyElements) {
|
observer.disconnect();
|
||||||
element.style.setProperty("display", originalDisplayValue, originalDisplayPriority);
|
style.remove();
|
||||||
|
style = null;
|
||||||
|
|
||||||
|
for (let element of stickyElements) {
|
||||||
|
element.classList.remove("hseSticky");
|
||||||
}
|
}
|
||||||
stickyElements = [];
|
stickyElements.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue