Jump to content

User:Amorymeltzer/hideSectionDesktop.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Amorymeltzer (talk | contribs) at 14:26, 12 July 2018 (Taken from User:BethNaught/hideSectionDesktop.js at Special:PermaLink/849928547 to shorten text and don't use document.ready). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
//Taken from [[User:BethNaught/hideSectionDesktop.js]] at [[Special:PermaLink/849928547]]
//Shorten text and don't use document.ready (creates ugly spaces with [[User:The Earwig/permalink.js]])

/**
 * 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 $ */

$(function() {
	// Helper functions
	function _toggleVisibility(elt) {
		if (elt.style.display == "none") {
			elt.style.display = "";
		} else {
			elt.style.display = "none";
		}
	}
	function _createToggleVisibilityLink(sectionID) {
		// Create HTML
		var out = document.createElement('span');
		out.classList.add('mw-editsection');
		
		var left = document.createElement('span');
		var right = document.createElement('span');
		left.classList.add('mw-editsection-bracket');
		right.classList.add('mw-editsection-bracket');
		var leftBracket = document.createTextNode("[");
		var rightBracket = document.createTextNode("]");
		left.appendChild(leftBracket);
		right.appendChild(rightBracket);
		
		var center  = document.createElement('a');
		center.classList.add('nonimage');
		center.title = 'Toggle visibility: ' + sectionID;
		var centerText = document.createTextNode("hide");
		center.appendChild(centerText);
		center._classToToggle = 'bn-hidesection-' + sectionID;
		
		out.appendChild(left);
		out.appendChild(center);
		out.appendChild(right);
		
		// Event listener
		center.addEventListener("click", function(){
			var eltsToToggle = document.getElementsByClassName(this._classToToggle);
			var numToToggle = eltsToToggle.length;
			for (var j = 0; j < numToToggle; j++){
				_toggleVisibility(eltsToToggle[j]);
			}
		});
		return out;
	}
	
	// BEGIN WORKFLOW
	// This hold the stuff that comes from wikitext, and that only
	var parserOutput = document.getElementById('mw-content-text').children[0];
	
	// Initialise loop variables
	var classifying = false;
	var sectionID;
	var loopLength = parserOutput.children.length;
	
	// Loop over children adding necessary stuff
	for (var i = 0; i < loopLength; i++){
		var currentChild = parserOutput.children[i];
		if (currentChild.nodeName.toLowerCase() == "h2") {
			classifying = true;  // all subsequent elements need modifying
			sectionID = currentChild.children[0].id;  // this is the sectionID
			var temp_a = _createToggleVisibilityLink(sectionID);
			currentChild.appendChild(temp_a);
		} else if (classifying) {
			currentChild.classList.add('bn-hidesection-' + sectionID);
		}
	}
});