Jump to content

MediaWiki:Common.js

From New Bradford
Revision as of 01:31, 31 October 2026 by Admin (talk | contribs) (1 revision imported)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Logged-out visitors: show the main menu (Bestiary, Tameables, etc.) pinned on the left,
   the same way logged-in users see it. The Vector skin only pins it for logged-in users. */
( function () {
	if ( mw.config.get( 'wgUserName' ) !== null ) {
		return;
	}
	function pinMainMenu() {
		var menu = document.getElementById( 'vector-main-menu' );
		var pinned = document.getElementById( 'vector-main-menu-pinned-container' );
		if ( !menu || !pinned || pinned.contains( menu ) ) {
			return;
		}
		pinned.appendChild( menu );
		var header = menu.querySelector( '.vector-pinnable-header' );
		if ( header ) {
			header.classList.remove( 'vector-pinnable-header-unpinned' );
			header.classList.add( 'vector-pinnable-header-pinned' );
		}
		var html = document.documentElement.classList;
		html.remove( 'vector-feature-main-menu-pinned-disabled' );
		html.add( 'vector-feature-main-menu-pinned-enabled' );
	}
	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', pinMainMenu );
	} else {
		pinMainMenu();
	}
}() );

/* Section share links: a small link icon after the page title and every section heading.
   Clicking it copies a direct link to that spot and updates the address bar. */
( function () {
	var css = '.nb-anchor{margin-left:.4em;font-size:.6em;vertical-align:middle;text-decoration:none!important;opacity:.45;cursor:pointer}' +
		'.nb-anchor:hover{opacity:1}' +
		'.nb-anchor svg{width:1em;height:1em;fill:currentColor;vertical-align:middle}';
	var icon = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3.9 12a3.1 3.1 0 0 1 3.1-3.1h4V7H7a5 5 0 0 0 0 10h4v-1.9H7A3.1 3.1 0 0 1 3.9 12zM8 13h8v-2H8v2zm9-6h-4v1.9h4a3.1 3.1 0 0 1 0 6.2h-4V17h4a5 5 0 0 0 0-10z"/></svg>';
	function makeLink( hash, label ) {
		var a = document.createElement( 'a' );
		a.className = 'nb-anchor';
		a.href = hash || location.pathname + location.search;
		a.title = 'Copy link to ' + label;
		a.setAttribute( 'aria-label', a.title );
		a.innerHTML = icon;
		a.addEventListener( 'click', function ( e ) {
			e.preventDefault();
			var url = location.origin + location.pathname + location.search + ( hash || '' );
			history.replaceState( null, '', url );
			if ( hash ) {
				var t = document.getElementById( hash.slice( 1 ) );
				if ( t ) { t.scrollIntoView(); }
			}
			if ( navigator.clipboard ) {
				navigator.clipboard.writeText( url ).then( function () {
					mw.notify( 'Link copied: ' + label );
				} );
			}
		} );
		return a;
	}
	function addAnchors() {
		if ( mw.config.get( 'wgAction' ) !== 'view' ) { return; }
		mw.util.addCSS( css );
		var title = document.getElementById( 'firstHeading' );
		if ( title && !title.querySelector( '.nb-anchor' ) ) {
			title.appendChild( makeLink( '', title.textContent.trim() ) );
		}
		var heads = document.querySelectorAll( '.mw-parser-output .mw-heading h2[id], .mw-parser-output .mw-heading h3[id], .mw-parser-output .mw-heading h4[id], .mw-parser-output .mw-heading h5[id], .mw-parser-output .mw-heading h6[id]' );
		heads.forEach( function ( h ) {
			if ( h.querySelector( '.nb-anchor' ) ) { return; }
			h.appendChild( makeLink( '#' + h.id, h.textContent.trim() ) );
		} );
	}
	mw.loader.using( [ 'mediawiki.util', 'mediawiki.notification' ] ).then( function () {
		$( addAnchors );
	} );
}() );