Jump to content

User:Fred Gandt/sectionFooters.js

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Fred Gandt (talk | contribs) at 18:27, 21 June 2024 (broke it while fixing it, like anyone but me cares). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.loader.load( "/w/index.php?title=User:Fred_Gandt/sectionFooters.css&action=raw&ctype=text/css", "text/css" );
$( document ).ready( () => {
	"use strict";
	const CONTENT = document.querySelector( "#mw-content-text .mw-parser-output" );
	if ( !CONTENT ) return;
	
	const SECTIONS = Array.from( CONTENT.querySelectorAll( "div.mw-heading" ) ),
		sectionLevel = section => parseInt( section.querySelector( "h2, h3, h4, h5, h6" ).tagName.replace( "h", "" ) );
	
	SECTIONS.forEach( ( section, i ) => {
		let section_level = sectionLevel( section ),
			section_clone = section.cloneNode( true ),
			next_section = SECTIONS.find( ( section, ii ) => ii > i && sectionLevel( section ) >= section_level ); // TODO why not work?
		section_clone.style.opacity = 0.5;
		if ( next_section ) {
			next_section.before( section_clone );
		} else {
			CONTENT.append( section_clone );
		}
	} );
} );