پرش به محتوا

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

از دانشنامه علوم انسانی
imported>Dalba
به‌روزرسانی با کمک https://fr.wikipedia.org/wiki/MediaWiki:Gadget-AncreTitres.js، جی‌کوئری، در حالت ویرایش نیز کار می‌کند. مشکل تداخلش حل شد، به همراه کد پیوند پایدار
imported>Ebrahim
عدم استفاده از mw.Uri
 
(۲۹ نسخهٔ میانی ویرایش شده توسط ۴ کاربر نشان داده نشد)
خط ۱: خط ۱:
// ==UserScript==
// @name        AncreTitres
// @namespace  https://fa.wikipedia.org/
// @description مدیاویکی:Gadget-AncreTitres.js
// @include    https://fa.wikipedia.org/*
// @version    1
// @grant      none
// ==/UserScript==
/*jslint browser:true */
/*global $, wgServer, wgScript, wgPageName */
/**
* AncreTitres
*
* Cette fonction fournit un lien vers une section de page en cliquant
* sur le lien [ancre] à droite du titre de section.
*
* Auteurs : Pabix, Phe, Bayo, Dalba -- Correctif apporté par Chphe
* Date de dernière révision : 16 October 2014
* {{Projet:JavaScript/Script|AncreTitres}}
*/
/**
/**
  * AncreTitres
  * AncreTitres
خط ۲۵: خط ۵:
  * sur le lien [URL] ou [[lien]] à droite du titre de section.
  * sur le lien [URL] ou [[lien]] à droite du titre de section.
  *
  *
  * Auteurs : Pabix, Phe, Bayo, Chphe, Arkanosis et Dalba
  * Auteurs : Pabix, Phe, Bayo, Chphe, Arkanosis, Mah3110, Ash_Crow, Dalba, Jeeputer
  * {{Projet:JavaScript/Script|AncreTitres}}
  * {{Projet:JavaScript/Script|AncreTitres}}
  */
  */
/*global alert, jQuery, mediaWiki, window */
/* global $, mw */
/*jslint vars: true, plusplus: true */
/* eslint-env browser */
// <nowiki>
// <nowiki>
(function (mw, $) {
mw.loader.using( [ 'mediawiki.util', 'user' ], function () {
  'use strict';
'use strict';
  $(function () {
 
    var _option = {
$( function ( $ ) {
      nom_permalink: '[پایاپیوند]',
var lang = mw.config.get( 'wgUserLanguage' ),
      nom_ancre: '[URL]',
messages = {
      nom_lien_interne: '[[ویکی‌پیوند]]',
en: {
      description_pl: 'نمایش پیوند پایدار به این بخش',
'ancretitres-anchor-name': '[URL]',
      description: 'نمایش یو‌آر‌ال این بخش',
'ancretitres-internal-link-name': '[[Link]]',
      descinterne: 'نمایش پیوند ویکی‌پدیایی این بخش',
'ancretitres-permanent-link-name': '[[Permalink]]',
      linkcolor: '',
'ancretitres-description': 'Get an URL to this section',
      fontSize: 'xx-small',
'ancretitres-int-description': 'Get an internal link to this section',
      fontWeight: 'normal',
'ancretitres-int-description': 'Get a permanent link to this section',
      afficheE: true,
'ancretitres-notif-title': 'Text copied to clipboard',
      afficheI: true,
'ancretitres-notif-error': 'Could not copy to clipboard'
      afficheP: true
},
    };
fa: {
    if (typeof window.AncreTitres !== 'undefined') {
'ancretitres-anchor-name': '[نشانی وب]',
      $.extend(_option, window.AncreTitres);
'ancretitres-internal-link-name': '[[ویکی‌پیوند]]',
    }
'ancretitres-permanent-link-name': '[[پایاپیوند]]',
    if (!$('#content').length || (!_option.afficheI && !_option.afficheE)) {
'ancretitres-description': 'دریافت نشانی وب برای این بخش',
      return;
'ancretitres-int-description': 'دریافت پیوند داخلی به این بخش',
    }
'ancretitres-permanent-description': 'دریافت پیوند پایدار به این بخش',
    $.each($('span.mw-headline'), function (_, headline) {
'ancretitres-notif-title': 'متن به بریده‌دان منتقل شد',
      var anchor = headline.getAttribute('id');
'ancretitres-notif-error': 'ناتوان از رونویسی در بریده‌دان'
      var $span = $('<span class="noprint ancretitres" style="' +
}
      'font-size: ' + _option.fontSize + '; ' +
},
      'font-weight: ' + _option.fontWeight + '; ' + (_option.linkcolor !== '' ? 'color: ' + _option.linkcolor + ';' : '') +
options = {
      '"></span>');
afficheE: true,
      if (_option.afficheP && $('#t-permalink a').length) {
afficheI: true,
        $span.append(' ');
afficheP: true
        $('<a href="#" title="' + _option.descinterne + '">' + _option.nom_permalink + '</a>').click(function () {
};
          window.prompt('پایاپیوند:', $('#t-permalink a') [0].href + '#' + anchor
 
          );
mw.messages.set( messages.en );
          return false;
if ( lang !== 'en' && lang in messages ) {
        }).appendTo($span);
mw.messages.set( messages[ lang ] );
      }
}
      if (_option.afficheE) {
 
        $span.append(' ');
function copyTextToClipboard( text ) {
        $('<a href="#" title="' + _option.description + '">' + _option.nom_ancre + '</a>').click(function () {
function notifySuccess() {
          window.prompt('نشانی وب معمولی:', 'https:' + mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + mw.config.get('wgPageName') + '#' + anchor
mw.notify( text, { title: mw.msg( 'ancretitres-notif-title' ), tag: 'ancretitres', type: 'info', autoHide: true } );
          );
}
          return false;
 
        }).appendTo($span);
function notifyError() {
      }
mw.notify( mw.msg( 'ancretitres-notif-error' ), { tag: 'ancretitres', type: 'error', autoHide: true } );
      if (_option.afficheI) {
}
        $span.append(' ');
 
        $('<a href="#" title="' + _option.descinterne + '">' + _option.nom_lien_interne + '</a>').click(function () {
if ( navigator.clipboard ) {
          window.prompt('ویکی‌پیوند:', '[[' + mw.config.get('wgPageName') + '#' + anchor + ']]'
// Clipboard API
          );
navigator.clipboard.writeText( text ).then( notifySuccess, notifyError );
          return false;
} else {
        }).appendTo($span);
notifyError();
      }
}
      $(headline).parent().append($span);
}
    });
 
  });
if ( typeof window.AncreTitres !== 'undefined' ) {
}(mediaWiki, jQuery));
Object.assign( options, window.AncreTitres );
// </nowiki>
}
 
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 ] );
}
 
} );
 
} );

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

/**
 * 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 ] );
		}

	} );

} );