core/resources/texttospeech/texttospeech.js
!function($){$.textToSpeech=function(options){var settings=$.extend({text:"",playEvent:"textToSpeech:play",pauseEvent:"textToSpeech:pause",resumeEvent:"textToSpeech:resume",stopEvent:"textToSpeech:stop",errorEvent:"textToSpeech:error",finishedPlayingEvent:"textToSpeech:finished",eventSelector:"document",language:"en",favoriteVoice:""},options);if(!("speechSynthesis"in window))return!1;with(speechSynthesis)return{utterance:null,isPaused:!1,checkIfSpeakingInterval:null,play:function(){var t=this;if(this.isPaused)return this.resume();this.utterance=new SpeechSynthesisUtterance(settings.text),this.utterance.onerror=function(e){console.error(e),t.stop(),$(settings.eventSelector).trigger(settings.errorEvent)},$(window).on("beforeunload",function(){window.speechSynthesis.cancel()});try{window.speechSynthesis.cancel(),new Promise(function(e,t){var n=window.speechSynthesis.getVoices();0!==n.length?e(n):window.speechSynthesis.addEventListener("voiceschanged",function(){n=window.speechSynthesis.getVoices(),e(n)})}).then(function(e){t.utterance&&(t.utterance.voice=e.filter(function(e){return e.lang===settings.language})[0],t.utterance.voice=e.filter(function(e){return e.name===settings.favoriteVoice})[0],t.utterance.volume=1,t.utterance.rate=1,t.utterance.pitch=1,t.utterance.lang=settings.language,t.checkIfSpeaking(),window.speechSynthesis.speak(t.utterance),$(settings.eventSelector).trigger(settings.playEvent))})}catch(e){console.warn(e),this.stop()}},pause:function(){this.isPaused=!0,window.speechSynthesis.pause(),$(settings.eventSelector).trigger(settings.pauseEvent)},resume:function(){this.isPaused&&(this.isPaused=!1,window.speechSynthesis.resume(),$(settings.eventSelector).trigger(settings.resumeEvent))},stop:function(){this.checkIfSpeakingInterval&&clearInterval(this.checkIfSpeakingInterval),this.isPaused=!1,window.speechSynthesis.cancel(),$(settings.eventSelector).trigger(settings.stopEvent)},checkIfSpeaking:function(){var e=this;this.checkIfSpeakingInterval=setInterval(function(){!e.utterance||e.isPaused||window.speechSynthesis.speaking||e.stop()},500)},setText:function(e){settings.text=e},getText:function(){return settings.text}}}}(jQuery);