MediaWiki:Common.js: Difference between revisions
Appearance
m 1 revision imported |
nb>Admin Add section share-link icons |
||
| Line 33: | Line 33: | ||
var css = '.nb-anchor{margin-left:.4em;font-size:.6em;vertical-align:middle;text-decoration:none!important;opacity:.45;cursor:pointer}' + | 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:hover{opacity:1}' + | ||
'.nb-anchor svg{width:1em;height:1em;fill:currentColor;vertical-align:middle}'; | '.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>'; | 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 ) { | function makeLink( hash, label ) { | ||
var a = document.createElement( 'a' ); | var a = document.createElement( 'a' ); | ||
| Line 62: | Line 44: | ||
a.addEventListener( 'click', function ( e ) { | a.addEventListener( 'click', function ( e ) { | ||
e.preventDefault(); | e.preventDefault(); | ||
var url = location.origin + location.pathname + location.search + ( hash || '' ); | var url = location.origin + location.pathname + location.search + ( hash || '' ); | ||
history.replaceState( null, '', url ); | |||
if ( navigator.clipboard | if ( hash ) { | ||
navigator.clipboard.writeText( url ).then( | 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 ); | |||
} ); | |||
} | } | ||
} ); | } ); | ||
| Line 75: | Line 60: | ||
function addAnchors() { | function addAnchors() { | ||
if ( mw.config.get( 'wgAction' ) !== 'view' ) { return; } | if ( mw.config.get( 'wgAction' ) !== 'view' ) { return; } | ||
mw.util.addCSS( css ); | |||
var title = document.getElementById( 'firstHeading' ); | var title = document.getElementById( 'firstHeading' ); | ||
if ( title && !title.querySelector( '.nb-anchor' ) ) { | if ( title && !title.querySelector( '.nb-anchor' ) ) { | ||
| Line 86: | Line 71: | ||
} ); | } ); | ||
} | } | ||
mw.loader.using( [ 'mediawiki.util', 'mediawiki.notification' ] ).then( function () { | |||
$( addAnchors ); | |||
} | } ); | ||
}() ); | }() ); | ||
Revision as of 01:40, 31 October 2026
/* 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 );
} );
}() );