User:SimonWikt/common.js

/*	Turns Wiktionary into a 'N'lingual Dictionary Collapses all content headings into a tree of HTML "Details" tags. The languages in "openLangs" are displayed open. The headings in "openDets" are displayed open. (function {  const sw = JSON.parse( localStorage.getItem("SimonWikt") );  if( !sw || !sw.collapseDisabled ) {		const openLangs = ["English", "Bulgarian"];	const openDets =		[ "Conjugation",		  "Declension",		  "Etymology 1", "Etymology 2", "Etymology 3", "Etymology 4",		  "Adjective", "Adverb",		  "Conjunction",		  "Determiner",		  "Interjection",		  "Letter",		  "Numeral", "Noun",		  "Particle", "Preposition", "Pronoun", "Proper noun",		  "Translations",		  "Verb"		];	if( document.querySelector("section") ) {		/* Mobile Wiktionary has 'section's */		/* and collapsible languages */		/* Headings are now in DIVs, so implement later */		document.querySelectorAll("#mw-content-text > div .mw-heading3").forEach( (h3)=>{ createDetailsLevel(h3) } );		document.querySelectorAll("#mw-content-text > div .mw-heading2").forEach( (h2)=>{ /* collapse unwanted languages */ const headingName = h2.querySelector( "H2" ).innerText; if( ! openLanguage( headingName ) ) h2.click; } );	} else		/* Collapse 'H2's and above, except wanted languages and headings */		document.querySelectorAll("#mw-content-text > div > h2").forEach( (h2)=>{ createDetailsLevel(h2) } );

function openLanguage( lang ) { return openLangs.indexOf( lang ) >= 0; }	function openDet( det ){ return openLanguage( det ) || openDets.indexOf( det ) >= 0; }

function createDetailsLevel( heading ) { function getHeadingInfo( heading ) { const headings = ["H0", "H1", "H2", "H3", "H4", "H5", "H6"]; /* Mobile Wiktionary headings are in a DIV */ let id = ""; let name = ""; let i = headings.indexOf( heading.nodeName ); if( heading.nodeName == "DIV" && heading.classList.contains("mw-heading") ) { heading = heading.children[0]; i = headings.indexOf( heading.nodeName ); id = heading.id; name = heading.innerText; }			else if ( i > 0 ) { let span = heading.querySelector( "span" ); id = span.id; name = span.innerText; }			return { level: i, id: id, name: name }; }		const headingInfo = getHeadingInfo( heading ); const level = headingInfo.level; let sib = heading.nextSibling; const dets = document.createElement( "details" ); heading.after( dets ); const summary = document.createElement( "summary" ); dets.appendChild( summary ); summary.appendChild( heading ); dets.open = openDet( headingInfo.name ); dets.title = headingInfo.id + " details"; while( sib ) { let nsib = sib.nextSibling; let sibLevel = getHeadingInfo( sib ).level; if( sibLevel > 0 ) { if( sibLevel > level ) { let subDets = createDetailsLevel( sib ); nsib = subDets.nextSibling; dets.appendChild( subDets ); }				else break; }			else dets.appendChild( sib ); sib = nsib; }		return dets; } } });