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