-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspeechkitt.min.js
6 lines (6 loc) · 3.49 KB
/
speechkitt.min.js
1
2
3
4
5
6
//! Speech KITT
//! version : 0.3.0
//! author : Tal Ater @TalAter
//! license : MIT
//! https://github.com/TalAter/SpeechKITT
(function(a){"use strict";var b,c,d,e,f,g,h=this,i=0,j=!1,k="Activate Voice Control",l="What can I help you with?",m=[],n=function(){g=document.createElement("div"),g.id="skitt-ui",g.innerHTML='<a id="skitt-toggle-button"> </a><label for="skitt-toggle-button" id="skitt-toggle-button__label">'+k+'</label><div id="skitt-listening-box"><div id="skitt-listening-text"><span id="skitt-listening-text__instructions">'+l+"</span></div></div>",g.style.display="none",document.body.appendChild(g),p(),o(),document.getElementById("skitt-toggle-button").addEventListener("click",function(){h.SpeechKITT.toggleRecognition()})},o=function(){e&&g&&(f?f.href=e:(f=document.createElement("link"),f.rel="stylesheet",f.href=e,f.id="skitt-style-sheet",document.body.appendChild(f)))},p=function(){if(m.length&&g){var a=document.getElementById("skitt-listening-text__samples");if(!a){var b=document.getElementById("skitt-listening-text__instructions");a=document.createElement("span"),a.id="skitt-listening-text__samples",b.parentNode.insertBefore(a,b.nextSibling)}a.innerText=m.join(". ")+"."}},q=function(){g!==a&&(g.classList.remove("skitt-ui--not-listening"),g.classList.add("skitt-ui--listening"))},r=function(){g!==a&&(g.classList.add("skitt-ui--not-listening"),g.classList.remove("skitt-ui--listening"))},s=function(){j||(j=!0,q())},t=function(){j&&(j=!1,r())},u=function(a,b){g&&(document.getElementById(b).innerText=a)},v=function(){var a=new Date;a.setTime(a.getTime()+6e4*i),document.cookie="skittremember=1; expires="+a.toUTCString()+"; path=/"},w=function(){document.cookie="skittremember=1; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"},x=function(){return-1!==document.cookie.indexOf("skittremember")};h.SpeechKITT={setStartCommand:function(a,b){if(a=h[a]||a,"function"!=typeof a)throw new TypeError("invalid callback function");b=b||this,c={callback:a,context:b}},setAbortCommand:function(a,b){if(a=h[a]||a,"function"!=typeof a)throw new TypeError("invalid callback function");b=b||this,d={callback:a,context:b}},startRecognition:function(){if(!c)throw new TypeError("cannot start recognition. Start command not defined");i&&v(),c.callback.apply(c.context),s()},abortRecognition:function(){if(!d)throw new TypeError("cannot abort recognition. Abort command not defined");w(),d.callback.apply(d.context),t()},toggleRecognition:function(){j?this.abortRecognition():this.startRecognition()},onStart:function(){h.clearTimeout(b),s()},onEnd:function(){b=setTimeout(t,100)},setStylesheet:function(a){e=a,o()},render:function(){g===a&&n(),x()&&!this.isListening()&&this.startRecognition(),this.isListening()?q():r()},vroom:function(){this.render()},hide:function(){if(g===a)throw new TypeError("cannot hide interface. Must be rendered first");g.classList.add("skitt-ui--hidden")},show:function(){if(g===a)throw new TypeError("cannot show interface. Must be rendered first");g.classList.remove("skitt-ui--hidden")},isListening:function(){return j},setToggleLabelText:function(a){k=a,u(a,"skitt-toggle-button__label")},setInstructionsText:function(a){l=a,u(a,"skitt-listening-text__instructions")},setSampleCommands:function(a){m=a,p()},rememberStatus:function(a){if("number"!=typeof a||0>a)throw new TypeError("rememberStatus() only accepts positive integers");i=a},annyang:function(){this.setStartCommand(annyang.start),this.setAbortCommand(annyang.abort),annyang.addCallback("start",this.onStart),annyang.addCallback("end",this.onEnd)}}}).call(this);