Медиавики:Gadget-yandex-speechrecognition.js

Мавод аз Википедиа — донишномаи озод

Эзоҳ: Баъди захира намудан, Шумо метавонед тағйиротҳои худро аз хотираи браузер гузариш карда, бубинед. Дар браузерҳои Mozilla / Firefox / Safari: тугмаи Shift-ро пахш намуда бо мушак Reload-ро пахш кунед, ё Ctrl-Shift-R-ро пахш намоед (Cmd-Shift-R барои компютерҳои Apple Mac); дар браузери IE: тугмаи Ctrl-ро пахш намуда бо мушак Refresh-ро пахш намоед, ё Ctrl-F5-ро пахш намоед; дар браузери Konqueror:: бо мушак Reload-ро пахш кунед, ё тугмаи F5-ро пахш намоед; дар браузери Opera ба Шумо пурра тоза кардани хотираи браузер ба воситаи Tools→Preferences лозим аст.

( function() {

	function addOldToolbarButton() {
		var $toolbar = $( '#gadget-toolbar' );
		if ( !$toolbar.length ) {
			$toolbar = $( '#toolbar' );
		}
		$( '<div>' ) //
		.addClass( 'mw-toolbar-editbutton' ) //
		.attr( 'id', 'mw-editbutton-gadget-speechrecognition' ) //
		.attr( 'alt', 'Распознавание речи' ) //
		.attr( 'title', 'Распознавание речи (Yandex SpeechKit Cloud)' ) //
		.css( 'background-image', 'url(//upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/200px-Loudspeaker.svg.png) ' ) //
		.appendTo( $toolbar ) //
		.on( 'click', switchRecognition );
	}

	function addNewToolbarButton() {
		$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
			'section': 'main',
			'group': 'format',
			'tools': {
				'wikilinker': {
					label: 'Распознавание речи (Yandex SpeechKit Cloud)',
					type: 'button',
					icon: '//upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/200px-Loudspeaker.svg.png',
					action: {
						type: 'callback',
						execute: function() {
							switchRecognition();
						}
					}
				}
			}
		} );
	}

	if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
		mediaWiki.loader.load( '//download.yandex.ru/webspeechkit/webspeechkit-1.0.0.js' );

		mw.loader.using( [ 'user.options', 'jquery.textSelection' ], function() {
			if ( mw.user.options.get( 'usebetatoolbar' ) === 1 ) {
				if ( mw.user.options.get( 'showtoolbar' ) === 1 ) {
					$.when( mw.loader.using( [ 'ext.wikiEditor', 'schema.Edit' ] ), $.ready ).then( addNewToolbarButton );
				}
			} else {
				mw.loader.using( 'mediawiki.toolbar', function() {
					$( addOldToolbarButton );
				} );
			}
		} );
	}

	var started = false;
	var speechRecognition = null;

	function switchRecognition() {

		if ( speechRecognition === null ) {
			speechRecognition = new ya.speechkit.SpeechRecognition();
		}

		if ( !started ) {
			speechRecognition.start( {
				apiKey: '5c49c6a8-bbd1-4429-9aec-b8d1d49f7edd',
				initCallback: function( sessionId, code ) {
					console.log( "[Yandex.SpeechRecognition Gadget] Session ID: " + sessionId );
					console.log( "[Yandex.SpeechRecognition Gadget] Code: " + code );
				},
				dataCallback: function( text, uttr, merge ) {
					console.log( "[Yandex.SpeechRecognition Gadget] Text: " + text );
					console.log( "[Yandex.SpeechRecognition Gadget] Uttr: " + uttr );
					console.log( "[Yandex.SpeechRecognition Gadget] Merge: " + merge );

					if ( uttr ) {
						$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
							peri: text,
							replace: true
						} ).textSelection( 'scrollToCaretPosition' ).focus();
					} else {
						// not the end of statement, reselect after replace
						var pos = $( '#wpTextbox1' ).textSelection( 'getCaretPosition' );
						var newLength = text.length;

						$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
							peri: text,
							replace: true
						} ).textSelection( 'setSelection', {
							start: pos,
							end: pos + newLength
						} );

					}
				},
				errorCallback: function( msg ) {
					console.log( "[Yandex.SpeechRecognition Gadget] Error: " + msg );
					started = false;
				},
				punctuation: true
			} );
			started = true;
			mw.notify( 'Распознавание текста запущено', {
				tag: 'speechRecognition'
			} );
		} else {
			speechRecognition.stop();
			started = false;
			mw.notify( 'Распознавание текста остановлено', {
				tag: 'speechRecognition'
			} );
		}
	}

} )();