-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpolyfill.min.js
7 lines (6 loc) · 3.17 KB
/
polyfill.min.js
1
2
3
4
5
6
7
/*!
* speech-synthesis v0.4.0
* The MIT License
* Copyright (c) 2014 Jan Antala <hello@janantala.com>
*/
!function(e,n){"use strict";var t=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 u="";r!=a.length&&(u=n[o]),e=(e+u).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 u=new RegExp(".{1,"+t+"}","g"),h=e.match(u);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},i=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 i,s,o=this,r={charIndex:void 0,elapsedTime:void 0,name:void 0},a=function(){s=(new Date).getTime(),r.elapsedTime=(s-(i||s))/1e3},u=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=new Audio;n.addEventListener("play",function(){a(),i?o.onresume&&o.onresume(r):(i=(new Date).getTime(),o.onstart&&o.onstart(r))},!1),n.addEventListener("ended",function(){if(e.length){var t=u(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 s=100;o.text.length>s?e=t(o.text,[".","!","?",":",";",","," "],s):e.push(o.text);var h=u(o.corsProxyServer,e.shift(),o.lang);return n.src=h,n.volume=o.volume,n.playbackRate=o.rate,n},this},s=function(){this.isPolyfill=!0,this.pending=!1,this.speaking=!1,this.paused=!1;var e=this,n=new Audio,t=[],i=function(t){var i=t.shift();e.speaking=!1,e.pending=t.length?!0:!1,i&&(n=i._initAudio(),s(n,i),u())},s=function(e,n){e.addEventListener("play",function(){},!1),e.addEventListener("ended",function(){n._ended&&i(t)},!1),e.addEventListener("error",function(){i(t)},!1),e.addEventListener("pause",function(){},!1)},o=function(n){e.pending=!0,t.push(n),e.speaking||e.paused||i(t)},r=function(){n.src="",n=void 0,n=new Audio,e.pending=!1,e.speaking=!1,e.paused=!1,t=[],i(t)},a=function(){n.pause(),e.speaking=!1,e.paused=!0},u=function(){n.src?(n.play(),e.speaking=!0):i(t),e.paused=!1},h=function(){return[]};return{isPolyfill:!0,pending:e.pending,speaking:e.speaking,paused:e.paused,speak:function(e){o(e)},cancel:function(){r()},pause:function(){a()},resume:function(){u()},getVoices:function(){h()}}},o=function(){return e.speechSynthesis&&e.SpeechSynthesisUtterance?!0:!1},r=function(){return o()?e.speechSynthesis:e.speechSynthesisPolyfill},a=function(){return o()?e.SpeechSynthesisUtterance:e.SpeechSynthesisUtterancePolyfill};e.SpeechSynthesisUtterancePolyfill=i,e.speechSynthesisPolyfill=new s,e.nativeSpeechSynthesisSupport=o,e.getSpeechSynthesis=r,e.getSpeechSynthesisUtterance=a}(window,document);