Jump to content

User:Fred Gandt/sectionFooters.js

From Wikipedia, the free encyclopedia
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.
$( 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/i, "" ) );
	
	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 still not quite right
		section_clone.style.opacity = 0.5;
		if ( next_section ) {
			next_section.before( section_clone );
		} else {
			CONTENT.append( section_clone );
		}
	} );
} );