User:Robin van der Vliet/Esperanto.js

From Wikisource
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
( function ( mw, $ ) {
	function run() {
		var isPage, useOldToolbar, useBetaToolbar, toolbarLib;
		mw.loader.using( 'user.options', function () {
			isPage = mw.config.get( 'wgCanonicalNamespace' ) === 'Page';
			useOldToolbar = mw.user.options.get( 'showtoolbar' ) === 1;
			useBetaToolbar = mw.user.options.get( 'usebetatoolbar' ) === 1;
			if ( isPage && ( useOldToolbar || useBetaToolbar ) ) {
				toolbarLib = useBetaToolbar ? 'ext.wikiEditor' : 'mediawiki.toolbar';
				mw.loader.using( [ 'mediawiki.api', toolbarLib ], function () {
					new mw.Api().then( customizeToolbar( useBetaToolbar ) );
				} );
			}
		} );
	}

	function customizeToolbar( useBeta ) {
		if ( ! useBeta && mw.toolbar ) {
			mw.toolbar.addButton( {
				imageFile: 'https://upload.wikimedia.org/wikipedia/commons/9/9a/Button_wiktionary.png',
				imageId: 'EsperantoButton'
			} );
			$("img#EsperantoButton").on('click', convertText);
		}

		if ( useBeta ) {
			$( document ).ready( function () {
				var esperantoButtonDetails = {
					type: 'button',
					icon: 'https://upload.wikimedia.org/wikipedia/commons/2/2f/Toolbaricon_content_transliteration_stop_sr.svg',
					labelMsg: 'Fix Esperanto text',
					action: { type: 'callback', execute: convertText }
				};
				var esperantoButton = {
					section: 'main', // 'proofreadpage-tools',
					group: 'insert', // 'other',
					tools: { 'EsperantoButton': esperantoButtonDetails }
				};
				$( "#wpTextbox1" ).wikiEditor( 'addToToolbar', esperantoButton );
			} );
		}
	}

	function convertText() {
		$( '#wpTextbox1' ).val( $( '#wpTextbox1' ).val().replace(/-\n/g , "").replace(/\n/g , " ").replace(/č/g , "ĉ").replace(/ć/g , "ĉ").replace(/ğ/g , "ĝ").replace(/ǔ/g , "ŭ") );

		for (i = 0; i < 10; i++) { 
			$( '#wpTextbox1' ).val( $( '#wpTextbox1' ).val().replace(/  /g , " ") );
		}

		$( '#wpTextbox1' ).val( $( '#wpTextbox1' ).val().replace(/^\s+|\s+$/g, '') );
	}

	run();
}( mediaWiki, jQuery ) );