-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.client.js
1 lines (1 loc) · 9.96 KB
/
test.client.js
1
!function(e){var t={};function o(n){if(t[n])return t[n].exports;var a=t[n]={i:n,l:!1,exports:{}};return e[n].call(a.exports,a,a.exports,o),a.l=!0,a.exports}o.m=e,o.c=t,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var a in e)o.d(n,a,function(t){return e[t]}.bind(null,a));return n},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="/",o(o.s=2)}([function(module,exports,__webpack_require__){__webpack_require__(1);const VoiceRecognition=annyang;class AlexClient{constructor(...config){config.length&&(this.commands=config[0].concat([{commands:["+","-","*","/","pi"],onMatch:(data,match,allData)=>{if(!1!==this.options.math){data=data.replace(/divided by/g,"/"),data=data.replace(/ million/g,"000000"),data=data.replace(/ billion/g,"000000000"),data=data.replace(/ trillion/g,"000000000000"),data=data.replace(/pi/g,"3.14159265359");try{this.localDispatch("calculator",parseInt(eval(data).toFixed(0),10))}catch(e){this.localDispatch("error","Sorry, I'm not able to translate "+data+" into a formula for you. Please try again.")}}}},{commands:["who is the","who are the","who is","who are","whos the","whos"],onMatch:e=>{!1!==this.options.wiki&&(e=e.replace("'",""),this.localDispatch("who is",e))}},{commands:["what is the","what are the","what is","what are","whats the","whats"],onMatch:e=>{!1!==this.options.wiki&&(e=e.replace("'",""),this.localDispatch("what is",e))}},{commands:["search for","google for","search","google"],onMatch:(e,t)=>{"string"==typeof this.options.search?this.search(e.replace(t,"").replace(/ /g,"+"),{engine:this.options.search}):"boolean"==typeof this.options.search&&!1!==this.options.search&&this.say("Only a search engine url, or false value can be passed to the search config option.")}},{commands:["find"],onMatch:(e,t)=>{this.find(e,{})}},{commands:["open"],request:(e,t)=>{this.open(e.replace(t,"").replace(/ /g,""))}}]),this.options=Object.assign({math:!0,wiki:!0,XHRUrl:"/",port:"8025",find:"/",search:"//www.google.com"},config[1]?config[1]:{}),this.commands.length?(this.latLng=[],navigator.geolocation.getCurrentPosition(e=>{this.latLng=[e.coords.latitude,e.coords.longitude]}),this.commands.length&&this.init()):this.say("Alex installed, but no commands found."))}say(e,t){"object"==typeof e&&(e=e[Math.floor(Math.random()*e.length)]),this.localDispatch("say",e,()=>{"function"==typeof t&&t()})}run(e,t,o){this.localDispatch(e,t,e=>{"function"==typeof o&&o(e)})}ask(e,t,o){this.say(e,()=>{this.listenForFeedback(t,e=>{"function"==typeof o&&(this.listenForCommands(),o(e))})})}find(e,t,o){this.localDispatch("find",e,e=>{this.say(e,()=>{this.listenForFeedback(t,e=>{"yes"===e&&this.localDispatch("batchOpen",[query,e],()=>{this.listenForCommands(),"function"==typeof o&&o()})})})})}search(e,t,o){this.localDispatch("search",e,e=>{this.say(e,()=>{"function"==typeof o&&o(e)})})}open(e,t,o){this.localDispatch("open",e,e=>{this.say(e,()=>{"function"==typeof o&&o(e)})})}localDispatch(e,t,o){const n=new XMLHttpRequest;n.open("GET","http://localhost:"+this.options.port+this.options.XHRUrl+"?task="+e+"&data="+t,!0),n.onreadystatechange=(()=>{4==n.readyState&&200==n.status&&"function"==typeof o&&o(n.responseText),VoiceRecognition.start()}),VoiceRecognition.abort(),n.send()}remoteDispatch(e,t){const o=new XMLHttpRequest;o.open("GET",e,!0),o.onreadystatechange=(()=>{4==o.readyState&&200==o.status&&"function"==typeof t&&t(o.responseText),VoiceRecognition.start()}),VoiceRecognition.abort(),o.send()}listenForCommands(e){VoiceRecognition.removeCallback("result"),VoiceRecognition.addCallback("result",e=>{let t=e[0].toLowerCase().trim();t.replace("alex","").length&&(t=t.replace("alex",""));let o=!1,n=[],a=[];Object.keys(this.commands).map(r=>{n=this.commands[r],a=n.commands,!0!==o&&Object.keys(a).map(r=>{!0!==o&&-1!==t.indexOf(a[r])&&("function"==typeof n.onMatch&&n.onMatch(t,a[r],e),o=!0)})})}),VoiceRecognition.start(),"function"==typeof e&&e()}listenForFeedback(e,t){e=Object.assign({retry:3,retryInterval:1e4,retryMessages:[],givenupMessages:["Perhaps we should try this again later."],cancels:[],cancelMessages:["Ok.","Sure.","Absolutly.","Stopped."]},e);let o=void 0;e.retryMessages.length&&(o=setTimeout(()=>{let n=e.retryMessages[Math.floor(Math.random()*e.retryMessages.length)];0===e.retry&&(n=e.givenupMessages[Math.floor(Math.random()*e.givenupMessages.length)]),this.localDispatch("error",n,()=>{e.retry>0?("int"==typeof e.retry&&(e.retry=e.retry-1),this.listenForFeedback(t,e)):(clearTimeout(o),this.listenForCommands())})},e.retryInterval)),VoiceRecognition.removeCallback("result"),VoiceRecognition.addCallback("result",n=>{n=n[0].toLowerCase().trim();let a=!1;e.cancels.length&&Object.keys(e.cancels).map(t=>{-1!==n.indexOf(e.cancels[t])&&(a=!0)}),"function"==typeof t&&(clearTimeout(o),a?this.localDispatch("error",e.cancelMessages[Math.floor(Math.random()*e.cancelMessages.length)],()=>{this.listenForCommands()}):t(n))}),VoiceRecognition.start()}init(){this.listenForCommands()}}module.exports=AlexClient},function(e,t,o){"use strict";var n,a,r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};a="undefined"!=typeof window?window:void 0,void 0===(n=function(){return a.annyang=function(e,t){var o,n=e.SpeechRecognition||e.webkitSpeechRecognition||e.mozSpeechRecognition||e.msSpeechRecognition||e.oSpeechRecognition;if(!n)return null;var a,i,s=[],c={start:[],error:[],end:[],soundstart:[],result:[],resultMatch:[],resultNoMatch:[],errorNetwork:[],errorPermissionBlocked:[],errorPermissionDenied:[]},l=0,h=0,u=!1,p="font-weight: bold; color: #00f;",d=!1,f=!1,g=/\s*\((.*?)\)\s*/g,m=/(\(\?:[^)]+\))\?/g,y=/(\(\?)?:\w+/g,b=/\*\w+/g,w=/[\-{}\[\]+?.,\\\^$|#]/g,v=function(e){for(var t=arguments.length,o=Array(1<t?t-1:0),n=1;n<t;n++)o[n-1]=arguments[n];e.forEach(function(e){e.callback.apply(e.context,o)})},k=function(){return a!==t},M=function(e,t){-1!==e.indexOf("%c")||t?console.log(e,t||p):console.log(e)},x=function(){k()||o.init({},!1)},R=function(e,t,o){s.push({command:e,callback:t,originalPhrase:o}),u&&M("Command successfully loaded: %c"+o,p)},S=function(e){var t;v(c.result,e);for(var o=0;o<e.length;o++){t=e[o].trim(),u&&M("Speech recognized: %c"+t,p);for(var n=0,a=s.length;n<a;n++){var r=s[n],i=r.command.exec(t);if(i){var l=i.slice(1);return u&&(M("command matched: %c"+r.originalPhrase,p),l.length&&M("with parameters",l)),r.callback.apply(this,l),void v(c.resultMatch,t,r.originalPhrase,e)}}}v(c.resultNoMatch,e)};return o={init:function(r){var p=!(1<arguments.length&&arguments[1]!==t)||arguments[1];a&&a.abort&&a.abort(),(a=new n).maxAlternatives=5,a.continuous="http:"===e.location.protocol,a.lang="en-US",a.onstart=function(){f=!0,v(c.start)},a.onsoundstart=function(){v(c.soundstart)},a.onerror=function(e){switch(v(c.error,e),e.error){case"network":v(c.errorNetwork,e);break;case"not-allowed":case"service-not-allowed":i=!1,(new Date).getTime()-l<200?v(c.errorPermissionBlocked,e):v(c.errorPermissionDenied,e)}},a.onend=function(){if(f=!1,v(c.end),i){var e=(new Date).getTime()-l;(h+=1)%10==0&&u&&M("Speech Recognition is repeatedly stopping and starting. See http://is.gd/annyang_restarts for tips."),e<1e3?setTimeout(function(){o.start({paused:d})},1e3-e):o.start({paused:d})}},a.onresult=function(e){if(d)return u&&M("Speech heard, but annyang is paused"),!1;for(var t=e.results[e.resultIndex],o=[],n=0;n<t.length;n++)o[n]=t[n].transcript;S(o)},p&&(s=[]),r.length&&this.addCommands(r)},start:function(e){x(),d=(e=e||{}).paused!==t&&!!e.paused,i=e.autoRestart===t||!!e.autoRestart,e.continuous!==t&&(a.continuous=!!e.continuous),l=(new Date).getTime();try{a.start()}catch(e){u&&M(e.message)}},abort:function(){i=!1,h=0,k()&&a.abort()},pause:function(){d=!0},resume:function(){o.start()},debug:function(){var e=!(0<arguments.length&&arguments[0]!==t)||arguments[0];u=!!e},setLanguage:function(e){x(),a.lang=e},addCommands:function(t){var o,n;for(var a in x(),t)if(t.hasOwnProperty(a))if("function"==typeof(o=e[t[a]]||t[a]))R((n=(n=a).replace(w,"\\$&").replace(g,"(?:$1)?").replace(y,function(e,t){return t?e:"([^\\s]+)"}).replace(b,"(.*?)").replace(m,"\\s*$1?\\s*"),new RegExp("^"+n+"$","i")),o,a);else{if(!("object"===(void 0===o?"undefined":r(o))&&o.regexp instanceof RegExp)){u&&M("Can not register command: %c"+a,p);continue}R(new RegExp(o.regexp.source,"i"),o.callback,a)}},removeCommands:function(e){e===t?s=[]:(e=Array.isArray(e)?e:[e],s=s.filter(function(t){for(var o=0;o<e.length;o++)if(e[o]===t.originalPhrase)return!1;return!0}))},addCallback:function(o,n,a){var r=e[n]||n;"function"==typeof r&&c[o]!==t&&c[o].push({callback:r,context:a||this})},removeCallback:function(e,o){var n=function(e){return e.callback!==o};for(var a in c)c.hasOwnProperty(a)&&(e!==t&&e!==a||(c[a]=o===t?[]:c[a].filter(n)))},isListening:function(){return f&&!d},getSpeechRecognizer:function(){return a},trigger:function(e){o.isListening()?(Array.isArray(e)||(e=[e]),S(e)):u&&M(f?"Speech heard, but annyang is paused":"Cannot trigger while annyang is aborted")}}}(a)}.apply(t,[]))||(e.exports=n)},function(e,t,o){const n=new(o(0))([{commands:["alex"],onMatch:(e,t,o)=>{n.say(["Yes.","How can I help?","Howdy!"])}},{commands:["hello world"],onMatch:(e,t,o)=>{n.say("Sending XHR request to run server side command, hello world",()=>{n.dispatch("hello world",null,()=>{n.say("Wow, that was a fast trip")})})}},{commands:["feedback test"],onMatch:(e,t,o)=>{n.say("Ok, lets do a feedback test.",()=>{n.dispatch("feedback test",null,()=>{n.say("Wow, that was a fast trip")})})}}],{base:"/",url:"/",port:"8025"})}]);