Jump to content
Wikipedia The Free Encyclopedia

User:BethNaught/hideSectionDesktop.js

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 user script seems to have a documentation page at User:BethNaught/hideSectionDesktop.
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.
 /**
  * This script adds a [toggle visibility] link to level 2 section headers.
  * The link does what it says on the tin.
  * No promise of accuracy or non-brokenness made.
  * Tested in Vector and Monobook. Last test: 2018年07月12日.

  * To install, add the following to your commmon.js or skin.js:

  mw.loader.load('//en.wikipedia.org/w/index.php?title=User:BethNaught/hideSectionDesktop.js&action=raw&ctype=text/javascript'); // Backlink: [[User:BethNaught/hideSectionDesktop.js]]

  * Author: [[User:BethNaught]]

  * Category: [[Category:Wikipedia scripts]]
  */

 /** global $ */

 $(document).ready(function(){
 // Helper functions
 function_toggleVisibility(elt){
 if(elt.style.display=="none"){
 elt.style.display="";
 }else{
 elt.style.display="none";
 }
 }
 function_createToggleVisibilityLink(sectionID){
 // Create HTML
 varout=document.createElement('span');
 out.classList.add('mw-editsection');

 varleft=document.createElement('span');
 varright=document.createElement('span');
 left.classList.add('mw-editsection-bracket');
 right.classList.add('mw-editsection-bracket');
 varleftBracket=document.createTextNode("[");
 varrightBracket=document.createTextNode("]");
 left.appendChild(leftBracket);
 right.appendChild(rightBracket);

 varcenter=document.createElement('a');
 center.classList.add('nonimage');
 center.title='Toggle visibility: '+sectionID;
 varcenterText=document.createTextNode("toggle visibility");
 center.appendChild(centerText);
 center._classToToggleSelector='.bn-hidesection-'+sectionID;

 out.appendChild(left);
 out.appendChild(center);
 out.appendChild(right);

 // Event listener
 center.addEventListener("click",function(){
 $(this._classToToggleSelector).each(function(){
 _toggleVisibility(this);
 });
 });
 returnout;
 }

 // BEGIN WORKFLOW
 // This holds the stuff that comes from wikitext, and that only
 varparserOutput=document.getElementsByClassName('mw-parser-output')[0];

 if(parserOutput){
 // Initialise loop variables
 varclassifying=false;
 varsectionID=0;
 vartemp_a,currentChild;
 varloopLength=parserOutput.children.length;

 // Loop over children adding necessary stuff
 for(vari=0;i<loopLength;i++){
 currentChild=parserOutput.children[i];
 if(currentChild.nodeName.toLowerCase()=="h2"){
 classifying=true;// all subsequent elements need modifying
 sectionID+=1;
 temp_a=_createToggleVisibilityLink(sectionID);
 currentChild.appendChild(temp_a);
 }elseif(classifying){
 currentChild.classList.add('bn-hidesection-'+sectionID);
 }
 }
 }
 });

AltStyle によって変換されたページ (->オリジナル) /