پرش به محتوا

مدیاویکی:Gadget-AncreTitres.js: تفاوت میان نسخه‌ها

از دانشنامه علوم انسانی
imported>Dalba
AncreTitres.afficheP
imported>Ebrahim
عدم استفاده از mw.Uri
 
(۳۰ نسخهٔ میانی ویرایش شده توسط ۴ کاربر نشان داده نشد)
خط ۱: خط ۱:
/*jslint browser:true */
/*global $, wgServer, wgScript, wgPageName */
/**
/**
  * AncreTitres
  * AncreTitres
  *
  *
  * Cette fonction fournit un lien vers une section de page en cliquant
  * Cette fonction fournit un lien vers une section de page en cliquant
  * sur le lien [ancre] à droite du titre de section.
  * sur le lien [URL] ou [[lien]] à droite du titre de section.
  *
  *
  * Auteurs : Pabix, Phe, Bayo, Dalba -- Correctif apporté par Chphe
  * Auteurs : Pabix, Phe, Bayo, Chphe, Arkanosis, Mah3110, Ash_Crow, Dalba, Jeeputer
* Date de dernière révision : 16 October 2014
  * {{Projet:JavaScript/Script|AncreTitres}}
  * {{Projet:JavaScript/Script|AncreTitres}}
  */
  */
var AncreTitres = {
/* global $, mw */
    nom_permalink: "[پایاپیوند]",
/* eslint-env browser */
    nom_ancre: '[URL]',
// <nowiki>
    nom_lien_interne: '[[ویکی\u200Cپیوند]]',
mw.loader.using( [ 'mediawiki.util', 'user' ], function () {
    description_pl: 'نمایش پیوند پایدار به این بخش',
'use strict';
    description: 'نمایش یو\u200Cآر\u200Cال این بخش',
    descinterne: 'نمایش پیوند ویکی\u200Cپدیایی این بخش',
    linkcolor: '',
    fontSize: 'xx-small',
    fontWeight: 'normal',
    afficheE: true,
    afficheI: true,
    afficheP: true
};


function ancrer() {
$( function ( $ ) {
    'use strict';
var lang = mw.config.get( 'wgUserLanguage' ),
    if (!document.getElementById('content')) {
messages = {
        return;
en: {
    }
'ancretitres-anchor-name': '[URL]',
    var titres = document.getElementsByTagName('span'),
'ancretitres-internal-link-name': '[[Link]]',
        i = 0,
'ancretitres-permanent-link-name': '[[Permalink]]',
        titre_courant,
'ancretitres-description': 'Get an URL to this section',
        ancre_nom,
'ancretitres-int-description': 'Get an internal link to this section',
        span,
'ancretitres-int-description': 'Get a permanent link to this section',
        link,
'ancretitres-notif-title': 'Text copied to clipboard',
        prefixedServer;
'ancretitres-notif-error': 'Could not copy to clipboard'
    if (!AncreTitres.afficheI && !AncreTitres.afficheE && !AncreTitres.afficheP) {
},
        return;
fa: {
    }
'ancretitres-anchor-name': '[نشانی وب]',
    prefixedServer = wgServer;
'ancretitres-internal-link-name': '[[ویکی‌پیوند]]',
    if (wgServer.substr(0, 2) === '//') {
'ancretitres-permanent-link-name': '[[پایاپیوند]]',
        if (document.URL.substr(0, 5) === 'http:') {
'ancretitres-description': 'دریافت نشانی وب برای این بخش',
            prefixedServer = 'http:' + wgServer;
'ancretitres-int-description': 'دریافت پیوند داخلی به این بخش',
        } else if (document.URL.substr(0, 6) === 'https:') {
'ancretitres-permanent-description': 'دریافت پیوند پایدار به این بخش',
            prefixedServer = 'https:' + wgServer;
'ancretitres-notif-title': 'متن به بریده‌دان منتقل شد',
        }
'ancretitres-notif-error': 'ناتوان از رونویسی در بریده‌دان'
    }
}
    for (i = 0; i < titres.length; i += 1) {
},
        titre_courant = titres[i].parentNode;
options = {
        if ((titres[i].className.match(/mw-headline/gi))) {
afficheE: true,
            ancre_nom = titres[i].id;
afficheI: true,
            span = document.createElement('span');
afficheP: true
            if (AncreTitres.linkcolor !== '') {
};
                span.style.color = AncreTitres.linkcolor;
 
            }
mw.messages.set( messages.en );
            span.style.fontSize = AncreTitres.fontSize;
if ( lang !== 'en' && lang in messages ) {
            span.style.fontWeight = AncreTitres.fontWeight;
mw.messages.set( messages[ lang ] );
            span.className = 'noprint ancretitres';
}
            if (AncreTitres.afficheP) {
 
                link = document.createElement('a');
function copyTextToClipboard( text ) {
                link.innerHTML = AncreTitres.nom_permalink;
function notifySuccess() {
                link.title = AncreTitres.description_pl;
mw.notify( text, { title: mw.msg( 'ancretitres-notif-title' ), tag: 'ancretitres', type: 'info', autoHide: true } );
                link.setAttribute('ancre', $('#t-permalink a')[0].href + '#' + ancre_nom);
}
                link.setAttribute('onclick', 'window.prompt(' + '\'پایاپیوند: \', this.getAttribute(\'ancre\'));');
 
                span.appendChild(document.createTextNode(' '));
function notifyError() {
                span.appendChild(link);
mw.notify( mw.msg( 'ancretitres-notif-error' ), { tag: 'ancretitres', type: 'error', autoHide: true } );
            }
}
            if (AncreTitres.afficheE) {
 
                link = document.createElement('a');
if ( navigator.clipboard ) {
                link.innerHTML = AncreTitres.nom_ancre;
// Clipboard API
                link.title = AncreTitres.description;
navigator.clipboard.writeText( text ).then( notifySuccess, notifyError );
                link.setAttribute('ancre', prefixedServer + wgScript + '?title=' + wgPageName + '#' + ancre_nom);
} else {
                link.setAttribute('onclick', 'window.prompt(' + '\'نشانی وب معمولی: \', this.getAttribute(\'ancre\'));');
notifyError();
                span.appendChild(document.createTextNode(' '));
}
                span.appendChild(link);
}
            }
 
            if (AncreTitres.afficheI) {
if ( typeof window.AncreTitres !== 'undefined' ) {
                link = document.createElement('a');
Object.assign( options, window.AncreTitres );
                link.innerHTML = AncreTitres.nom_lien_interne;
}
                link.title = AncreTitres.descinterne;
 
                link.setAttribute('ancre', '[[' + wgPageName + '#' + ancre_nom + ']]');
if ( !options.afficheI && !options.afficheE && !options.afficheP ) {
                link.setAttribute('onclick', 'window.prompt(' + '\'ویکی‌پیوند: \', this.getAttribute(\'ancre\'));');
return;
                span.appendChild(document.createTextNode(' '));
}
                span.appendChild(link);
 
            }
// https://phabricator.wikimedia.org/T13555
            titre_courant.appendChild(span);
// https://www.mediawiki.org/wiki/Heading_HTML_changes
        }
 
    }
// old markup (hN > .mw-headline)
}
$( '.mw-headline' ).each( function ( _, mwHeadline ) {
$(ancrer);
if ( mwHeadline.id ) {
processHeading( mwHeadline.parentElement, mwHeadline.id );
}
} );
 
// new markup (.mw-heading > hN)
$( '.mw-heading' ).each( function ( _, mwHeading ) {
var heading = mwHeading.querySelector( 'h1, h2, h3, h4, h5, h6' );
if ( heading && heading.id ) {
processHeading( mwHeading, heading.id );
}
} );
 
function processHeading( headingContainer, sectionId ) {
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 = new URL(mw.util.getUrl(), window.location).toString() + '#' + sectionId;
 
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 = sectionId
// 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 );
}
if ( options.afficheP ) {
var $linkP = $( '<a>' )
.attr( 'href', sectionUrl )
.attr( 'title', mw.msg( 'ancretitres-permanent-description' ) )
.text( mw.msg( 'ancretitres-permanent-link-name' ) )
.click( function ( e ) {
e.preventDefault();
var api = new mw.Api(),
apiParams = {
action: 'query',
prop: 'revisions',
titles:  mw.config.get( 'wgPageName' ),
rvprop: 'ids',
rvslots: 'main',
formatversion: '2',
format: 'json'
},
escapedAnchor = sectionId
// 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;' ),
revid;
api.get( apiParams ).done( function ( data ) {
revid = data.query.pages[0].revisions[0].revid
var outputText = '[[ویژه:پیوند دائمی/' + revid + '#' + ( escapedAnchor ).replaceAll( '_', ' ' ) + ']]';
copyTextToClipboard( outputText );
});
} );
$span.append( ' ', $linkP );
}
 
headingContainer.append( $span[ 0 ] );
}
 
} );
 
} );

نسخهٔ کنونی تا ۵ مارس ۲۰۲۵، ساعت ۰۷:۳۷

/**
 * 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, Dalba, Jeeputer
 * {{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-permanent-link-name': '[[Permalink]]',
					'ancretitres-description': 'Get an URL to this section',
					'ancretitres-int-description': 'Get an internal link to this section',
					'ancretitres-int-description': 'Get a permanent link to this section',
					'ancretitres-notif-title': 'Text copied to clipboard',
					'ancretitres-notif-error': 'Could not copy to clipboard'
				},
				fa: {
					'ancretitres-anchor-name': '[نشانی وب]',
					'ancretitres-internal-link-name': '[[ویکی‌پیوند]]',
					'ancretitres-permanent-link-name': '[[پایاپیوند]]',
					'ancretitres-description': 'دریافت نشانی وب برای این بخش',
					'ancretitres-int-description': 'دریافت پیوند داخلی به این بخش',
					'ancretitres-permanent-description': 'دریافت پیوند پایدار به این بخش',
					'ancretitres-notif-title': 'متن به بریده‌دان منتقل شد',
					'ancretitres-notif-error': 'ناتوان از رونویسی در بریده‌دان'
				}
			},
			options = {
				afficheE: true,
				afficheI: true,
				afficheP: 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 && !options.afficheP ) {
			return;
		}

		// https://phabricator.wikimedia.org/T13555
		// https://www.mediawiki.org/wiki/Heading_HTML_changes

		// old markup (hN > .mw-headline)
		$( '.mw-headline' ).each( function ( _, mwHeadline ) {
			if ( mwHeadline.id ) {
				processHeading( mwHeadline.parentElement, mwHeadline.id );
			}
		} );

		// new markup (.mw-heading > hN)
		$( '.mw-heading' ).each( function ( _, mwHeading ) {
			var heading = mwHeading.querySelector( 'h1, h2, h3, h4, h5, h6' );
			if ( heading && heading.id ) {
				processHeading( mwHeading, heading.id );
			}
		} );

		function processHeading( headingContainer, sectionId ) {
			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 = new URL(mw.util.getUrl(), window.location).toString() + '#' + sectionId;

			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 = sectionId
							// 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 );
			}
			
			if ( options.afficheP ) {
				var $linkP = $( '<a>' )
					.attr( 'href', sectionUrl )
					.attr( 'title', mw.msg( 'ancretitres-permanent-description' ) )
					.text( mw.msg( 'ancretitres-permanent-link-name' ) )
					.click( function ( e ) {
						e.preventDefault();
						var api = new mw.Api(),
						apiParams = {
							action: 'query',
							prop: 'revisions',
							titles:  mw.config.get( 'wgPageName' ),
							rvprop: 'ids',
							rvslots: 'main',
							formatversion: '2',
							format: 'json'
						},
						escapedAnchor = sectionId
							// 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;' ),
						revid;
						api.get( apiParams ).done( function ( data ) {
							revid = data.query.pages[0].revisions[0].revid
							var outputText = '[[ویژه:پیوند دائمی/' + revid + '#' + ( escapedAnchor ).replaceAll( '_', ' ' ) + ']]';
							copyTextToClipboard( outputText );
						});
					} );
				$span.append( ' ', $linkP );
			}

			headingContainer.append( $span[ 0 ] );
		}

	} );

} );