User:Phlsph7/UnfoldedNumberedTOC(Vector2022).js
Appearance
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 user script seems to have a documentation page at User:Phlsph7/UnfoldedNumberedTOC(Vector2022). |
/*** Unfolded and numbered TOC for Vector 2022 ***/
// Unfolds the table of contents 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 skin 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;
gap: 0.25em;
display: flex;
}
.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 sections
const listElements = document.getElementsByClassName('vector-toc-level-1');
for(var listElement of listElements){
listElement.classList.add('vector-toc-list-item-expanded');
}
// avoid bug showing multiple columns for the use of italics
const tocTextElements = document.getElementsByClassName('vector-toc-text');
for(var tocTextElement of tocTextElements){
// put everything after the number into a separate span element
tocTextElement.innerHTML = tocTextElement.innerHTML.replace('</span>', '</span><span>') + '</span>';
}
}
})();