User:Urvaxhi/speechToText.js

From Meta, a Wikimedia project coordination wiki

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.
/*
    @Author [[User:JayPrakash12345]]
    @Author [[User:Urvaxhi]]
    @OwnBy [[meta:Indic-TechCom]]
    
    Speech Recognition using Web Speech APIs to convert speech to text in the WikiEditor
*/
var SpeechRecognitionWiki = function () {

        $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
        'section': 'main',
        'group': 'insert',
        'tools': {
            'SpeechRecognitionWiki': {
                label: 'Speech Recognition',
                type: 'button',
                icon: '//upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Ic_mic_none_48px.svg/25px-Ic_mic_none_48px.svg.png',
                action: {
                    type: 'callback',
                    execute: function () {
                                                contentLang = mw.config.get('wgContentLanguage');
                                                window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition;
                                                const recognition = new SpeechRecognition();
                                                recognition.lang = contentLang;
                                                var text = $( '#wpTextbox1' ).val();

                                                recognition.start();
                                                recognition.onresult = function (event) {
                                                    var speechToText = event.results[0][0].transcript;
                                              
                                                    $( '#wpTextbox1' ).val( text + ' ' + speechToText );
                                                };
                    }
                }
            }
        }
    } );
};

/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
                $.when( mw.loader.using( 'ext.wikiEditor' ), $.ready)
                 .then( SpeechRecognitionWiki );
}