User:Chlod/Scripts/FoldArchives.js
Appearance
< User:Chlod | Scripts
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Chlod/Scripts/FoldArchives. |
// Fold Archives
// Author: Chlod
// Version: 1.0.0-REL
// Makes archived threads foldable to reduce taken screen space.
mw.hook('wikipage.content').add(function() {
// Prevent from running twice.
if (document.body.getAttribute("data-archives-folded") != null)
return;
document.body.toggleAttribute("data-archives-folded", true);
var prefix = window.folda_prefix || "folda-";
var maxHeight = window.folda_maxHeight || "50vh";
var fadeGradient = window.folda_gradient || (window.folda_dark ? "linear-gradient(to bottom, transparent 70%, rgba(0,0,0,0.3) 90%, rgba(0,0,0,0.75))" : "linear-gradient(to bottom, transparent 75%, white 95%, white)")
var archivedElements = document.querySelectorAll(".mw-body-content .boilerplate.archived");
mw.util.addCSS(`.${prefix}collapsed { height: ${maxHeight}; overflow: hidden; position: relative; }`);
mw.util.addCSS(`.${prefix}collapsed .${prefix}expander { user-select: none; display: block; bottom: 0; left: 0; position: absolute; width: 100%; height: 100%; background: ${fadeGradient}; color: black; text-align: center; padding-top: calc(${maxHeight} - 1.5rem); line-height: 1.5rem; font-style: italic; box-sizing: border-box; cursor: pointer; }`);
archivedElements.forEach(function (element) {
// Prevent collapsing already-small archives.
var oldHeight = element.style.height;
var oldHeightValue = element.clientHeight;
element.style.height = maxHeight;
var newHeightValue = element.clientHeight;
element.style.height = "";
if (newHeightValue > oldHeightValue) {
// We're only going to make it bigger. Give up here.
return;
}
element.classList.add(`${prefix}collapsed`);
var expander = document.createElement("div");
expander.classList.add(`${prefix}expander`);
expander.innerText = "Click to expand";
expander.onclick = () => {
expander.parentElement.classList.remove(`${prefix}collapsed`);
expander.parentElement.classList.add(`${prefix}expanded`);
expander.parentElement.removeChild(expander);
}
element.appendChild(expander);
});
// Scroll back to anchor if available due to scrolling changes
if (mw.config.get("wgNamespaceNumber") !== -1 && window.location.hash && window.location.hash.length > 0)
document.getElementById(window.location.hash.substr(1)).scrollIntoView();
});