-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathannyang.min.js
6 lines (6 loc) · 3.4 KB
/
annyang.min.js
1
2
3
4
5
6
//! annyang
//! version : 2.2.1
//! author : Tal Ater @TalAter
//! license : MIT
//! https://www.TalAter.com/annyang/
(function(a){"use strict";var b=this,c=b.SpeechRecognition||b.webkitSpeechRecognition||b.mozSpeechRecognition||b.msSpeechRecognition||b.oSpeechRecognition;if(!c)return b.annyang=null,a;var d,e,f=[],g={start:[],error:[],end:[],result:[],resultMatch:[],resultNoMatch:[],errorNetwork:[],errorPermissionBlocked:[],errorPermissionDenied:[]},h=0,i=!1,j="font-weight: bold; color: #00f;",k=!1,l=!1,m=/\s*\((.*?)\)\s*/g,n=/(\(\?:[^)]+\))\?/g,o=/(\(\?)?:\w+/g,p=/\*\w+/g,q=/[\-{}\[\]+?.,\\\^$|#]/g,r=function(a){return a=a.replace(q,"\\$&").replace(m,"(?:$1)?").replace(o,function(a,b){return b?a:"([^\\s]+)"}).replace(p,"(.*?)").replace(n,"\\s*$1?\\s*"),new RegExp("^"+a+"$","i")},s=function(a){var b=Array.prototype.slice.call(arguments,1);a.forEach(function(a){a.callback.apply(a.context,b)})},t=function(){return d!==a},u=function(){t()||b.annyang.init({},!1)},v=function(a,c,d){f.push({command:a,callback:c,originalPhrase:d}),i&&b.console.log("Command successfully loaded: %c"+d,j)};b.annyang={init:function(m,n){n=n===a?!0:!!n,d&&d.abort&&d.abort(),d=new c,d.maxAlternatives=5,d.continuous="http:"===b.location.protocol,d.lang="en-US",d.onstart=function(){l=!0,s(g.start)},d.onerror=function(a){switch(s(g.error),a.error){case"network":s(g.errorNetwork);break;case"not-allowed":case"service-not-allowed":e=!1,s((new Date).getTime()-h<200?g.errorPermissionBlocked:g.errorPermissionDenied)}},d.onend=function(){if(l=!1,s(g.end),e){var a=(new Date).getTime()-h;1e3>a?setTimeout(b.annyang.start,1e3-a):b.annyang.start()}},d.onresult=function(a){if(k)return i&&b.console.log("Speech heard, but annyang is paused"),!1;for(var c=a.results[a.resultIndex],d=[],e=0;e<c.length;e++)d[e]=c[e].transcript;s(g.result,d);for(var h,l=0;l<d.length;l++){h=d[l].trim(),i&&b.console.log("Speech recognized: %c"+h,j);for(var m=0,n=f.length;n>m;m++){var o=f[m],p=o.command.exec(h);if(p){var q=p.slice(1);return i&&(b.console.log("command matched: %c"+o.originalPhrase,j),q.length&&b.console.log("with parameters",q)),o.callback.apply(this,q),s(g.resultMatch,h,o.originalPhrase,d),!0}}}return s(g.resultNoMatch,d),!1},n&&(f=[]),m.length&&this.addCommands(m)},start:function(c){k=!1,u(),c=c||{},e=c.autoRestart!==a?!!c.autoRestart:!0,c.continuous!==a&&(d.continuous=!!c.continuous),h=(new Date).getTime();try{d.start()}catch(f){i&&b.console.log(f.message)}},abort:function(){e=!1,t()&&d.abort()},pause:function(){k=!0},resume:function(){b.annyang.start()},debug:function(a){i=arguments.length>0?!!a:!0},setLanguage:function(a){u(),d.lang=a},addCommands:function(a){var c;u();for(var d in a)if(a.hasOwnProperty(d))if(c=b[a[d]]||a[d],"function"==typeof c)v(r(d),c,d);else{if(!("object"==typeof c&&c.regexp instanceof RegExp)){i&&b.console.log("Can not register command: %c"+d,j);continue}v(new RegExp(c.regexp.source,"i"),c.callback,d)}},removeCommands:function(b){return b===a?void(f=[]):(b=Array.isArray(b)?b:[b],void(f=f.filter(function(a){for(var c=0;c<b.length;c++)if(b[c]===a.originalPhrase)return!1;return!0})))},addCallback:function(c,d,e){if(g[c]!==a){var f=b[d]||d;"function"==typeof f&&g[c].push({callback:f,context:e||this})}},removeCallback:function(b,c){var d=function(a){return a.callback!==c};for(var e in g)g.hasOwnProperty(e)&&(b!==a&&b!==e||(c===a?g[e]=[]:g[e]=g[e].filter(d)))},isListening:function(){return l&&!k},getSpeechRecognizer:function(){return d}}}).call(this);