MediaWiki:Gadget-AncreTitres.js

Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
/**
 * AncreTitres
 *
 * Cette fonction fournit un lien vers une section de page en cliquant
 * sur le lien [URL] ou [[lien]] à droite du titre de section.
 *
 * Auteurs : Pabix, Phe, Bayo, Chphe, Arkanosis, Mah3110, Ash_Crow
 * {{Projet:JavaScript/Script|AncreTitres}}
 */
/* global $, mw */
/* eslint-env browser */
// <nowiki>
mw.loader.using( [ 'mediawiki.util', 'user' ], function () {
	'use strict';

	$( function ( $ ) {
		var lang = mw.config.get( 'wgUserLanguage' ),
			messages = {
				en: {
					'ancretitres-anchor-name': '[URL]',
					'ancretitres-internal-link-name': '[[Link]]',
					'ancretitres-description': 'Get an URL to this section',
					'ancretitres-int-description': 'Get an internal link to this section',
					'ancretitres-notif-title': 'Text copied to clipboard',
					'ancretitres-notif-error': 'Could not copy to clipboard'
				},
				fr: {
					'ancretitres-anchor-name': '[URL]',
					'ancretitres-internal-link-name': '[[Lien]]',
					'ancretitres-description': 'Obtenir une URL vers cette section',
					'ancretitres-int-description': 'Obtenir un lien interne vers cette section',
					'ancretitres-notif-title': 'Texte copié dans le presse-papiers',
					'ancretitres-notif-error': 'Impossible de copier dans le presse-papiers'
				}
			},
			options = {
				afficheE: true,
				afficheI: true
			};

		mw.messages.set( messages.en );
		if ( lang !== 'en' && lang in messages ) {
			mw.messages.set( messages[ lang ] );
		}

		function copyTextToClipboard( text ) {
			function notifySuccess() {
				mw.notify( text, { title: mw.msg( 'ancretitres-notif-title' ), tag: 'ancretitres', type: 'info', autoHide: true } );
			}

			function notifyError() {
				mw.notify( mw.msg( 'ancretitres-notif-error' ), { tag: 'ancretitres', type: 'error', autoHide: true } );
			}

			if ( navigator.clipboard ) {
				// Clipboard API
				navigator.clipboard.writeText( text ).then( notifySuccess, notifyError );
			} else {
				notifyError();
			}
		}

		if ( typeof window.AncreTitres !== 'undefined' ) {
			Object.assign( options, window.AncreTitres );
		}

		if ( !options.afficheI && !options.afficheE ) {
			return;
		}

		$( 'span.mw-headline' ).each( function ( _, headline ) {
			var $span = $( '<span>' )
				.addClass( 'noprint ancretitres' )
				.css( {
					'font-size': 'xx-small',
					'font-weight': 'normal',
					'user-select': 'none' // jQuery se charge d'ajouter un vendor prefix si nécessaire
				} );

			var sectionUrl = 'https://' + mw.config.get( 'wgServerName' ) + mw.util.getUrl() + '#' + headline.id;

			if ( options.afficheE ) {
				var $linkE = $( '<a>' )
					.attr( 'href', sectionUrl )
					.attr( 'title', mw.msg( 'ancretitres-description' ) )
					.text( mw.msg( 'ancretitres-anchor-name' ) )
					.click( function ( e ) {
						e.preventDefault();
						copyTextToClipboard( sectionUrl );
					} );
				$span.append( ' ', $linkE );
			}

			if ( options.afficheI ) {
				var $linkI = $( '<a>' )
					.attr( 'href', sectionUrl )
					.attr( 'title', mw.msg( 'ancretitres-int-description' ) )
					.text( mw.msg( 'ancretitres-internal-link-name' ) )
					.click( function ( e ) {
						e.preventDefault();
						var escapedAnchor = headline.id
							// escaping caractères spéciaux HTML
							// (partiel, '"& ne sont pas escapés pour ne pas dégrader inutilement la lisibilité du lien)
							.replaceAll( '<', '&lt;' )
							.replaceAll( '>', '&gt;' )
							// escaping caractères spéciaux MediaWiki
							.replaceAll( '[', '&#91;' )
							.replaceAll( ']', '&#93;' )
							.replaceAll( '{', '&#123;' )
							.replaceAll( '|', '&#124;' )
							.replaceAll( '}', '&#125;' );
						var outputText = '[[' + ( mw.config.get( 'wgPageName' ) + '#' + escapedAnchor ).replaceAll( '_', ' ' ) + ']]';
						copyTextToClipboard( outputText );
					} );
				$span.append( ' ', $linkI );
			}

			$( headline ).parent().append( $span );
		} );

	} );

} );