|
31 | 31 | </div> |
32 | 32 | <% }) %> |
33 | 33 | </div> |
34 | | - <form action="add" method="post" id="question-form"> |
35 | | - <input type="text" placeholder="Send a message." name="inputQuestion" value="" id="input-question"> |
36 | | - <button type="submit" onclick="this.innerHTML = '<i class=\'fa fa-spinner fa-pulse\'></i>';"><i class="fa fa-paper-plane"></i></button> |
37 | | - </form> |
| 34 | + <div class="messaging"> |
| 35 | + <button onclick="onRecordButtonClick();" id="mic-btn"><i class="fa fa-microphone"></i></button> |
| 36 | + <form action="add" method="post" id="question-form"> |
| 37 | + <input type="text" placeholder="Send a message." name="inputQuestion" value="" id="input-question"> |
| 38 | + <button type="submit" onclick="this.innerHTML = '<i class=\'fa fa-spinner fa-pulse\'></i>';" id="question-send-btn"><i class="fa fa-paper-plane"></i></button> |
| 39 | + </form> |
| 40 | + </div> |
38 | 41 | </div> |
39 | 42 | <div class="settings-nav" id="settings-div"> |
40 | 43 | <div class="settings-nav-inside"> |
|
48 | 51 | </div> |
49 | 52 | </div> |
50 | 53 | <script> |
| 54 | + // Updates the chat div such that it is at the bottom always (or rather every millisecond) so that user need not scroll each time new answer. |
| 55 | + window.setInterval(function() { |
| 56 | + var elem = document.getElementById("chat"); |
| 57 | + elem.scrollTop = elem.scrollHeight; |
| 58 | + }, 1); |
| 59 | + |
51 | 60 | |
52 | 61 | function speak() { |
53 | 62 | var unescaped_answer = new DOMParser().parseFromString("<%= data[data.length - 1].answer%>", "text/html").documentElement.textContent; |
|
77 | 86 | } |
78 | 87 | |
79 | 88 | |
| 89 | + function onRecordButtonClick() { |
| 90 | + var micButton = document.getElementById("mic-btn"); |
| 91 | + micButton.style.backgroundColor = "red"; |
| 92 | + micButton.style.color = "white"; |
| 93 | + micButton.style.border = "1.5px solid red"; |
| 94 | + micButton.style.borderRadius = "50%"; |
| 95 | + |
| 96 | + |
| 97 | + var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition; |
| 98 | + var recognition = new SpeechRecognition(); |
| 99 | + |
| 100 | + recognition.onstart = function() { |
| 101 | + |
| 102 | + }; |
| 103 | + |
| 104 | + recognition.onspeechend = function() { |
| 105 | + recognition.stop(); |
| 106 | + micButton.style.backgroundColor = "#f7f7f8"; |
| 107 | + micButton.style.color = "gray"; |
| 108 | + micButton.style.border = "1.5px solid #dededf"; |
| 109 | + micButton.style.borderRadius = "15%"; |
| 110 | + }; |
| 111 | + |
| 112 | + |
| 113 | + recognition.onresult = function(event) { |
| 114 | + var transcript = event.results[0][0].transcript; |
| 115 | + var confidence = event.results[0][0].confidence; |
| 116 | + document.getElementById("input-question").value = transcript; |
| 117 | + document.getElementById("question-send-btn").click(); |
| 118 | + }; |
| 119 | + |
| 120 | + recognition.start(); // start recognition |
| 121 | + } |
| 122 | + |
| 123 | + |
80 | 124 | function openSettings() { |
81 | 125 | document.getElementById("settings-div").style.visibility = "visible"; |
82 | 126 | } |
|
0 commit comments