MediaWiki:Gadget-ShortenURL.js

Une nouvelle de Wikinews, la source d'informations que vous pouvez écrire.

Note : après avoir publié vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou appuyez sur Ctrl + F5 ou Ctrl + R (⌘ + R sur un Mac).
  • Google Chrome : appuyez sur Ctrl + Maj + R (⌘ + Shift + R sur un Mac).
  • Internet Explorer / Edge : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl + F5.
  • Opera : appuyez sur Ctrl + F5.
/**
 * Shortenurl
 *
 * Ce gadget ajoute un lien dans le menu outils permettant de générer une
 * url raccourcis d'un lien vers un site wikimedia (par défaut la page courante).
 * 
 * Ce gadget utilise l'API de l'extension Mediawiki UrlShortner disponible sur meta, cf
 * https://www.mediawiki.org/wiki/Extension:UrlShortener
 *
 * {{w:Projet:JavaScript/Script|ShortenUrl}}
 */
/* global $, mw */
/* eslint-env browser */
// <nowiki>
mw.loader.using( [ 'mediawiki.util', 'oojs-ui-windows', 'mediawiki.ForeignApi' ], function () {
	'use strict';

	$( function ( $ ) {
		var lang = mw.config.get( 'wgUserLanguage' ),
			messages = {
				en: {
					'shortenurl-portlet-text': 'Shorten URL',
					'shortenurl-portlet-tooltip': 'Shorten a link',
					'shortenurl-firstpopup-title': 'Enter a URL to shorten',
					'shortenurl-firstpopup-text': 'This must be part of the websites hosted by the WMF.',
					'shortenurl-firstpopup-shorten-label': 'Shorten',
					'shortenurl-firstpopup-cancel-label': 'Cancel',
					'shortenurl-secondpopup-text': 'Here you have the shorten link:',
					'shortenurl-secondpopup-close-label': 'Close',
					'shortenurl-notif-error-disallowed': 'The URL entered is not part of the Wikimedia sites and can not be shortened.',
					'shortenurl-notif-error-malformed': 'The URL is malformed.',
					'shortenurl-notif-error-toolong': 'The URL is too long.',
					'shortenurl-notif-error-ratelimit': 'Please wait a few moments before shortening more URLs.',
					'shortenurl-notif-error': 'An unknown error occurred.',
				},
				fr: {
					'shortenurl-portlet-text': 'Réduction d\'URL',
					'shortenurl-portlet-tooltip': 'Raccourcir un lien',
					'shortenurl-firstpopup-title': 'Saisissez une URL à raccourcir',
					'shortenurl-firstpopup-text': 'Celle-ci doit faire partie des sites hébergés par la WMF.',
					'shortenurl-firstpopup-shorten-label': 'Raccourcir',
					'shortenurl-firstpopup-cancel-label': 'Annuler',
					'shortenurl-secondpopup-text': 'Voici le lien raccourci :',
					'shortenurl-secondpopup-close-label': 'Fermer',
					'shortenurl-notif-error-disallowed': 'L\'URL saisie ne fait pas partie des sites Wikimedia et ne peut donc être raccourcie.',
					'shortenurl-notif-error-malformed': 'L\'URL saisie est mal formée.',
					'shortenurl-notif-error-toolong': 'L\'URL saisie est trop longue.',
					'shortenurl-notif-error-ratelimit': 'Veuillez attendre quelques intants avant de raccourcir plus d\'URLs.',
					'shortenurl-notif-error': 'Une erreur non répertoriée a eu lieu.',
				},
				de: {
					'shortenurl-portlet-text': 'Kürze URL',
					'shortenurl-portlet-tooltip': 'Kürze einen Link',
					'shortenurl-firstpopup-title': 'Füge einen URL zum kürzen ein',
					'shortenurl-firstpopup-text': 'Dieser muss von einer Websites der Wikimedia Foundation sein.',
					'shortenurl-firstpopup-shorten-label': 'Kürzen',
					'shortenurl-firstpopup-cancel-label': 'Abbrechen',
					'shortenurl-secondpopup-text': 'Hier ist der gekürzte Link:',
					'shortenurl-secondpopup-close-label': 'Schließen',
					'shortenurl-notif-error-disallowed': 'Der eingegebene URL ist nicht von einer Websites der Wikimedia Foundation sein und kann nicht gekürzt werden.',
					'shortenurl-notif-error-malformed': 'Der URL ist fehlerhaft.',
					'shortenurl-notif-error-toolong': 'Der URL ist zu lang.',
					'shortenurl-notif-error-ratelimit': 'Warte bitte einen Moment, bevor di mehr URLs kürzt.',
					'shortenurl-notif-error': 'Ein unbekannter Fehler istaufgetreten.',
				},
				ko: {
					'shortenurl-portlet-text': 'URL 축약',
					'shortenurl-portlet-tooltip': '링크를 축소합니다',
					'shortenurl-firstpopup-title': '축소할 URL을 입력하세요',
					'shortenurl-firstpopup-text': '위키미디어 재단이 제공하는 웹사이트여야 합니다.',
					'shortenurl-firstpopup-shorten-label': '축약',
					'shortenurl-firstpopup-cancel-label': '취소',
					'shortenurl-secondpopup-text': '축소된 URL을 확인하세요:',
					'shortenurl-secondpopup-close-label': '닫기',
					'shortenurl-notif-error-disallowed': '입력한 URL은 위키미디어 사이트가 아니므로 축소할 수 없습니다.',
					'shortenurl-notif-error-malformed': 'URL이 잘못 입력되었습니다.',
					'shortenurl-notif-error-toolong': 'URL이 너무 깁니다.',
					'shortenurl-notif-error-ratelimit': '다른 URL을 축소하기 전에 잠시만 기다려 주세요.',
					'shortenurl-notif-error': '알 수 없는 오류가 발생했습니다.',
				}
			};

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

		function openFirstPopup() {
			OO.ui.prompt( mw.msg( 'shortenurl-firstpopup-text' ), {
				title: mw.msg( 'shortenurl-firstpopup-title' ),
				size: 'large',
				textInput: { value: decodeURIComponent( window.location ) },
				actions:[ { label: mw.msg( 'shortenurl-firstpopup-cancel-label' ) }, { label: mw.msg( 'shortenurl-firstpopup-shorten-label' ), flags: [ 'progressive', 'primary' ], action: 'accept' } ]
			} ).done( function ( url ) {
				if ( url !== null ) {
					var api = new mw.ForeignApi( 'https://meta.wikimedia.org/w/api.php' );
					api.post( {
						action: 'shortenurl',
						format: 'json',
						url: url
					} ).then( function( response ) {
						openSecondPopup( response.shortenurl.shorturl );
					}, function( errorcode ) {
						var errorMessage = '';
						switch( errorcode ) {
							case 'urlshortener-error-disallowed-url':
								errorMessage = 'shortenurl-notif-error-disallowed';
								break;
							case 'urlshortener-error-malformed-url':
								errorMessage = 'shortenurl-notif-error-malformed';
								break;
							case 'urlshortener-url-too-long':
								errorMessage = 'shortenurl-notif-error-toolong';
								break;
							case 'urlshortener-ratelimit':
								errorMessage = 'shortenurl-notif-error-ratelimit';
								break;
							default:
								errorMessage = 'shortenurl-notif-error';
						}
						
						mw.notify( mw.msg( errorMessage ), { tag: 'shortenurl', type: 'error', autoHide: true } );
					} );
				}
			} );
		}

		function openSecondPopup( url ) {
			OO.ui.prompt( mw.msg( 'shortenurl-secondpopup-text' ), {
				textInput: { value: url, readOnly: true, autofocus: true },
				actions:[ { label: mw.msg( 'shortenurl-secondpopup-close-label' ) } ]
			} );
		}
		
		// Add a link to the sidebar to open the popup
		var portletLink = mw.util.addPortletLink(
			'p-tb',
			'#',
			mw.msg( 'shortenurl-portlet-text' ),
			't-shortenurl',
			mw.msg( 'shortenurl-portlet-tooltip' ),
			null,
			'#t-info'
		);
		$( portletLink ).on( 'click', function( e ) {
			e.preventDefault();
			openFirstPopup();
		} );	
	} );
} );