Jump to content

User:SimonWikt/common.js

From Wiktionary, the free dictionary

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

This JavaScript is executed for SimonWikt on every page load.


(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", "Participle", "Prefix", "Preposition", "Pronoun", "Proper noun",
		  "Suffix",
		  "Translations",
		  "Verb"
		];
	
	if( document.querySelector("section") ) {
		/* Mobile Wiktionary has 'section's */
		/* and collapsible languages */
		/* Headings are now in DIVs */
		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 .mw-heading2").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 and Desktop 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;
	}
  }
})();

SimonWikt = {
	
	sw: function() { return JSON.parse( localStorage.getItem("SimonWikt") ) },
	
	collapsible: function() { return !this.sw() || !this.sw().collapseDisabled },

	disableCollapse: function () {
		localStorage.setItem( "SimonWikt",JSON.stringify( {collapseDisabled:true} ) );
	},
	
	enableCollapse: function() {
		localStorage.setItem( "SimonWikt",JSON.stringify( {collapseDisabled:false} ) );
	}
}

if( document.querySelector("#mw-content-text > div .mw-heading2") )
	document.querySelector("#mw-content-text").insertAdjacentHTML(
		"afterbegin",
		'<a id="p-visibility-collapse" href="#visibility-collapse" ' +
			'onclick="javascript:SimonWikt.' +
			( SimonWikt.collapsible() ? 'disableCollapse()' : 'enableCollapse()' ) +
			';location.reload();return false;">' +
			( SimonWikt.collapsible() ? 'Show sections' : 'Collapse sections' ) +
		'</a>'
	);