Jump to content

User:Fred Gandt/sectionLinks.js

From Wikipedia, the free encyclopedia
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.
$( document ).ready( () => {
	// TODO while previewing etc. or on every page?
	const SCTNS = Array.from( $(":header") );
	if ( SCTNS && SCTNS.length ) {
		mw.loader.load( "/w/index.php?title=User:Fred_Gandt/sectionLinks.css&action=raw&ctype=text/css", "text/css" );
		const CNS = mw.config.get( "wgCanonicalNamespace" ),
			PN = mw.config.get( "wgPageName" ),
			TTL = mw.config.get( "wgTitle" ),
			CCNS = CNS ? `${CNS}:` : "",
			LO = location.origin,
			cycleSectionLinks = function( evt ) {
				evt.preventDefault();
				let i = this.sectionLinks.indexOf( this.textContent ) + ( evt.deltaY > 0 ? 1 : -1 );
				this.textContent = this.sectionLinks[ i > 3 ? 0 : ( i < 0 ? 3 : i ) ];
			};
		let stc;
		SCTNS.forEach( h => {
			stc = h.textContent;
			h.sectionLinks = [
				stc,
				`[[${CCNS}${TTL}#${stc}|]]`,
				`{{section link|${CCNS}${TTL}|${stc}}}`,
				`${LO}/wiki/${PN}#${mw.util.wikiUrlencode( stc )}`
			];
			h.addEventListener( "wheel", cycleSectionLinks );
		} );
	}
} );