forked from janantala/speech-synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyfill.min.js
7 lines (6 loc) · 3.5 KB
/
polyfill.min.js
1
2
3
4
5
6
7
/*!
* speech-synthesis v0.4.1
* The MIT License
* Copyright (c) 2014 Jan Antala <hello@janantala.com>
*/
!function(e,n){"use strict";var t;try{t=new Audio}catch(i){}if("undefined"!=typeof t){var s=function(e,n,t){var i=[],s=function(e,o){if(n[o]&&e.trim().length)if(e.indexOf(n[o])>-1){var r=1,a=e.split(n[o]);a.forEach(function(e){if(e.length){var c="";r!=a.length&&(c=n[o]),e=(e+c).trim()}e.length&&e.length<=t?i.push(e):s(e,o+1),r++})}else s(e,o+1);else if(e.length)for(var c=new RegExp(".{1,"+t+"}","g"),h=e.match(c);h.length>0;)i.push(h.shift().trim())};s(e,0);var o=[];return i.forEach(function(e){o.length?o[o.length-1].length+e.length+1<=t?o[o.length-1]+=" "+e:o.push(e):o.push(e)}),o},o=function(e){this.isPolyfill=!0,this.text=e||"",this.lang=n.documentElement.lang||"en-US",this.volume=1,this.rate=1,this.voiceURI="native",this.pitch=1,this.onstart=void 0,this.onend=void 0,this.onerror=void 0,this.onpause=void 0,this.onresume=void 0,this.onmark=void 0,this.onboundary=void 0,this.corsProxyServer="http://www.corsproxy.com/";var t,i,o=this,r={charIndex:void 0,elapsedTime:void 0,name:void 0},a=function(){i=(new Date).getTime(),r.elapsedTime=(i-(t||i))/1e3},c=function(e,n,t){return[e,"translate.google.com/translate_tts?ie=UTF-8&q=",encodeURIComponent(n),"&tl=",t].join("")};return this._initAudio=function(){var e=[];o._ended=!1;var n;try{n=new Audio}catch(i){}n.addEventListener("play",function(){a(),t?o.onresume&&o.onresume(r):(t=(new Date).getTime(),o.onstart&&o.onstart(r))},!1),n.addEventListener("ended",function(){if(e.length){var t=c(o.corsProxyServer,e.shift(),o.lang);n.src=t,n.play()}else a(),o._ended=!0,o.onend&&o.onend(r)},!1),n.addEventListener("error",function(){a(),o._ended=!0,o.onerror&&o.onerror(r)},!1),n.addEventListener("pause",function(){o._ended||(a(),o.onpause&&o.onpause(r))},!1);var h=100;o.text.length>h?e=s(o.text,[".","!","?",":",";",","," "],h):e.push(o.text);var u=c(o.corsProxyServer,e.shift(),o.lang);return n.src=u,n.volume=o.volume,n.playbackRate=o.rate,n},this},r=function(){this.isPolyfill=!0,this.pending=!1,this.speaking=!1,this.paused=!1;var e,n=this,t=[];try{e=new Audio}catch(i){console.error(i)}var s=function(t){var i=t.shift();n.speaking=!1,n.pending=t.length?!0:!1,i&&(e=i._initAudio(),o(e,i),h())},o=function(e,n){e.addEventListener("play",function(){},!1),e.addEventListener("ended",function(){n._ended&&s(t)},!1),e.addEventListener("error",function(){s(t)},!1),e.addEventListener("pause",function(){},!1)},r=function(e){n.pending=!0,t.push(e),n.speaking||n.paused||s(t)},a=function(){e.src="",e=void 0,e=new Audio,n.pending=!1,n.speaking=!1,n.paused=!1,t=[],s(t)},c=function(){e.pause(),n.speaking=!1,n.paused=!0},h=function(){e.src?(e.play(),n.speaking=!0):s(t),n.paused=!1},u=function(){return[]};return{isPolyfill:!0,pending:n.pending,speaking:n.speaking,paused:n.paused,speak:function(e){r(e)},cancel:function(){a()},pause:function(){c()},resume:function(){h()},getVoices:function(){u()}}},a=function(){return e.speechSynthesis&&e.SpeechSynthesisUtterance?!0:!1},c=function(){return a()?e.speechSynthesis:e.speechSynthesisPolyfill},h=function(){return a()?e.SpeechSynthesisUtterance:e.SpeechSynthesisUtterancePolyfill};"function"==typeof define&&define.amd?define([],function(){return{SpeechSynthesisUtterancePolyfill:o,speechSynthesisPolyfill:new r,nativeSpeechSynthesisSupport:a,getSpeechSynthesis:c,getSpeechSynthesisUtterance:h}}):(e.SpeechSynthesisUtterancePolyfill=o,e.speechSynthesisPolyfill=new r,e.nativeSpeechSynthesisSupport=a,e.getSpeechSynthesis=c,e.getSpeechSynthesisUtterance=h)}}(window,document);