Jump to content

User:Phlsph7/UnfoldedNumberedTOC(Vector2022).js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Phlsph7 (talk | contribs) at 18:17, 25 January 2023 (template literals for better readability). 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.
/*** Unfolded and numbered TOC for Vector 2022 ***/

// Unfolds the TOC and numbers the sections.
// This script is a modified version of the code found at https://greasyfork.org/en/scripts/458558-wikipedia-vector-2022-better-toc .
// It is licensed under the MIT License.

(function () {
	// check whether the vector 2022 is used
	if (document.body.classList.contains("skin-vector-2022")){
		// compose stylesheet contents
		var stylesheetContents = '';
		
		// show numbers
		stylesheetContents += `.vector-toc-numb {
			display: inline !important
		}`;

		// add space after numbers
		stylesheetContents += `.vector-toc-numb:after {
			content: " "
		}`;

		// hide unfold button
		stylesheetContents += `.vector-toc-level-1 > button {
			display: none !important;
		}`;

		// adjust spacing
		stylesheetContents += `.vector-toc-text {
			padding: 2px 0 !important;
			display: flex;
			justify-content: flex-start;
			gap: 0.25em
		}
		
		.vector-toc-list-item .vector-toc-list-item {
			padding-left: 1em !important;
		}
		
		.vector-toc .vector-toc-numb, .vector-toc .vector-toc-numb:after {
			display: inline !important; white-space: pre;
		}`
		
		// add stylesheet to document
		const stylesheet = document.createElement('style');
		stylesheet.innerHTML = stylesheetContents;
		document.head.append(stylesheet);
		
		// unfold the sections
		const listElements = document.getElementsByClassName('vector-toc-level-1');
		for(var listElement of listElements){
			listElement.classList.add('vector-toc-list-item-expanded');
		}
	}
})()