PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/files//1.0.0/jarvis.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 131 lines | 107 code | 14 blank | 10 comment | 19 complexity | de901817d9e43cdb3072e799000df578 MD5 | raw file
  1. /** Point Jarvis
  2. * @version : 1.0.0
  3. * @author : Raven Lagrimas | rjlagrimas08@gmail.com
  4. * @license : MIT
  5. * @link : http://ravenjohn.github.io/jarvis
  6. */
  7. (function(root){
  8. "use strict";
  9. var j = root.webkitSpeechRecognition ||
  10. root.mozSpeechRecognition ||
  11. root.msSpeechRecognition ||
  12. root.oSpeechRecognition ||
  13. root.SpeechRecognition;
  14. if(!j) throw new Error("Speech Recognition is not supported.");
  15. String.prototype.trim = function () {
  16. return this.replace(/^\s+|\s+$/g, "").replace(/\s{2,}/g, " ");
  17. };
  18. String.prototype.toRegExp = function() {
  19. return new RegExp('^' +
  20. this.replace(/[\-{}\[\]+?.,\\\^$|#]/g, '\\$&')
  21. .replace(/\s*\((.*?)\)\s*/g, '(?:$1)?')
  22. .replace(/(\(\?)?:\w+/g, function(match, optional) {
  23. return optional ? match : '([^\\s]+)';
  24. })
  25. .replace(/\*\w+/g, '(.*?)')
  26. .replace(/(\(\?:[^)]+\))\?/g, '\\s*$1?\\s*')
  27. + '$', 'i');
  28. };
  29. var upgrade = {
  30. name : "Jarvis",
  31. cmds : [],
  32. lang : "en-US",
  33. debug : !1,
  34. reqname : !1,
  35. continuous : !0,
  36. maxAlternatives : 5,
  37. learn : function(cmds){
  38. cmds&&(this.cmds=cmds);
  39. this.onresult = function(r){
  40. for(var i=0, n=r.results[r.resultIndex], k=n.length; i < k; i+=1){
  41. var g = n[i].transcript.trim();
  42. this.onrecognize&&this.onrecognize(g);
  43. if(this.debug){
  44. console.log('recognized : ' + g);
  45. }
  46. for(var l=0, m=this.cmds.length; l < m; l+=1){
  47. var x = ((this.reqname ? this.name+' ' : '('+this.reqname+') ')+this.cmds[l][0]).toRegExp().exec(g);
  48. if(x){
  49. this.cmds[l][1].apply(this,x.slice(1));
  50. if(this.debug){
  51. console.log('match found');
  52. }
  53. break;
  54. }
  55. }
  56. if(l!=m) break;
  57. this.onnomatch&&this.onnomatch(g);
  58. if(this.debug){
  59. console.log('match not found');
  60. }
  61. }
  62. };
  63. },
  64. onerror : function(e){
  65. if(e.error == "network"){
  66. throw new Error("Jarvis needs the Internet");
  67. }
  68. if(e.error == "not-allowed"){
  69. throw new Error("Jarvis was denied. X(");
  70. }
  71. if(e.error == "no-speech"){
  72. throw new Error("Please refresh. X(");
  73. }
  74. throw new Error("Unknown error : " + e.error);
  75. },
  76. setLanguage : function(l){
  77. this.lang = l;
  78. },
  79. addCommand : function(c){
  80. this.cmds.push(c);
  81. },
  82. setName : function(n){
  83. this.name = n;
  84. },
  85. setVoiceKey : function(k){
  86. this.voicerssKey = k;
  87. },
  88. speak : function(t){
  89. if(typeof t !== "string"){
  90. throw new Error('Jarvis cannot speak other datatype, strings only.');
  91. }
  92. if(t.length > 100){
  93. throw new Error('Sorry but Jarvis is limited to speaking up to 100 characters only.');
  94. }
  95. if(!this.voicerssKey){
  96. throw new Error("Voice Rss Key is missing. You can get it from http://www.voicerss.org/");
  97. }
  98. var spkr = document.getElementById("_jarvis");
  99. if(!spkr){
  100. var audio = document.createElement('audio');
  101. audio.setAttribute('id', '_jarvis');
  102. audio.setAttribute('autoplay', 'autoplay');
  103. document.body.appendChild(audio);
  104. spkr = document.getElementById("_jarvis");
  105. }
  106. t = t.toLowerCase().replace(/\./g,' ').trim();
  107. // we'll use Google translate for the voice hoping that they won't take it down
  108. // if you happen to find a TTS API that sounds like jarvis, inform me ASAP
  109. document.getElementById("_jarvis").src = 'http://api.voicerss.org/?key='+this.voicerssKey+'&language='+this.lang+'&src='+encodeURIComponent(t);
  110. }
  111. };
  112. //expose this object to the world
  113. root.jarvis = new j();
  114. //upgrade jarvis
  115. for(var i in upgrade)
  116. root.jarvis[i] = upgrade[i];
  117. })(this);