User:Polygnotus/Scripts/CollapseSections.js
Appearance
From Wikipedia, the free encyclopedia
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 code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/CollapseSections.
/*** Collapse Sections ***/ // Stolen from User:BrandonXLF/CollapseSections.js with some minor tweaks // Adds an arrow to sections to make them collapsible // Documentation at [[en:w:User:BrandonXLF/CollapseSections]] // By [[en:w:User:BrandonXLF]] // Modified to work with new heading markup (MediaWiki 1.43+) // window.collapseSections - Set to true to collapse all sections by default mw.hook('wikipage.content').add(function(content){ if(mw.config.get('skin')==='minerva')return; mw.util.addCSS('[class*="hide-sec"]{display:none!important}'); // Only select h2's with id inside mw-heading mw-heading2 divs content.find('.mw-parser-output .mw-heading.mw-heading2:has(h2[id])').each(function(){ var$element=$(this); varheading=$element.find('h2[id]').first(); if(heading.length===0)return; // Skip empty headings if(heading.children().length===0&&heading.text().trim()==='')return; varlevel=2;// Always level 2 since we only process h2 varicon=$('<i class="mw-ui-icon-before mw-ui-icon-small mw-ui-icon mw-ui-icon-collapse"></i>'); icon.click(function(){ // Build selector for headings of equal or higher level varlevelMatch='.mw-heading.mw-heading1:has(h1[id]), .mw-heading.mw-heading2:has(h2[id])'; icon.toggleClass('mw-ui-icon-collapse'); icon.toggleClass('mw-ui-icon-expand'); // Toggle visibility for content after this heading up to the next same/higher level heading $element.nextUntil(levelMatch).toggleClass('hide-sect-h2'); }); if(window.collapseSections)icon.click(); heading.prepend(icon); }); }); mw.loader.load(['mediawiki.ui.icon','oojs-ui.styles.icons-movement']);