|
| 1 | +var submit_button = document.getElementById('submit'); |
| 2 | +var start_button = document.getElementById('start'); |
| 3 | +var ss_button = document.getElementById('ss'); |
| 4 | +var is_google_meet = false; |
| 5 | +var spanClassName = "CNusmb"; |
| 6 | +var div = document.getElementById('span_text'); |
| 7 | +var idx=1; |
| 8 | +var catch_meet = false; |
| 9 | +var google_meet_transcript_data = ""; |
| 10 | +var take_snapshot = false; |
| 11 | + |
| 12 | +function get_data(){ |
| 13 | + //console.log('came here'); |
| 14 | + var idx = 1; |
| 15 | + var spans = document.getElementsByClassName("CNusmb"); |
| 16 | + // console.log(spans); |
| 17 | + var take_snapshot = false; |
| 18 | + var global_text = ""; |
| 19 | + for(let i=0;i<spans.length;i++){ |
| 20 | + console.log(spans[i].innerText,spans[i].classList.contains('from-cs')); |
| 21 | + if(!spans[i].classList.contains('from-cs')){ |
| 22 | + var text = spans[i].innerText; |
| 23 | + var res = text.split(" "); |
| 24 | + //console.log(res); |
| 25 | + for(let i=0;i<res.length;i++){ |
| 26 | + if(res[i] == "take" || res[i]=="Take"){ |
| 27 | + if((i!=res.length-1) && (res[i+1]=="snapshot" || res[i+1]=="snapshot.")){ |
| 28 | + console.log('detected take snapshot') |
| 29 | + take_snapshot = true; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + global_text += text; |
| 34 | + global_text += " "; |
| 35 | + // console.log(text); |
| 36 | + // div.textContent += text; |
| 37 | + spans[i].classList.add('from-cs'); |
| 38 | + } |
| 39 | + } |
| 40 | + return {"text":global_text,"flag":take_snapshot}; |
| 41 | +} |
| 42 | + |
| 43 | +function start_fetching_meet(){ |
| 44 | + console.log('started'); |
| 45 | + // while(true){ |
| 46 | + if(catch_meet){ |
| 47 | + //console.log('inside'); |
| 48 | + setTimeout(() => { |
| 49 | + chrome.tabs.executeScript(null, { code: '(' + get_data + ')();' }, (results) => { |
| 50 | + // console.log('FUN RES'); |
| 51 | + // console.log(results); |
| 52 | + // div.innerText += " "; |
| 53 | + // div.innerText += results; |
| 54 | + google_meet_transcript_data += " "; |
| 55 | + google_meet_transcript_data += results[0].text; |
| 56 | + console.log(results[0].flag, results, "value of take snapshot bool"); |
| 57 | + if(results[0].flag){ |
| 58 | + console.log('take snapshot!! :)'); |
| 59 | + takeScreenShot(); |
| 60 | + acknowledgeSSSuccess(); |
| 61 | + } |
| 62 | + start_fetching_meet(); |
| 63 | + }); |
| 64 | + },5000); |
| 65 | + }else return; |
| 66 | + // } |
| 67 | +} |
| 68 | + |
| 69 | +function get_host_name(){ |
| 70 | + return window.location.host; |
| 71 | +} |
| 72 | + |
| 73 | +start_button.addEventListener("click",async function(){ |
| 74 | + localStorage.setItem('images',JSON.stringify([])); |
| 75 | + await chrome.tabs.executeScript(null, { code: '(' + get_host_name + ')();' }, (results) => { |
| 76 | + console.log(results); |
| 77 | + if(results[0] == "meet.google.com"){ |
| 78 | + is_google_meet = true; |
| 79 | + console.log('It is a G Meet'); |
| 80 | + } |
| 81 | + if(is_google_meet){ |
| 82 | + catch_meet = true; |
| 83 | + console.log('starting recording...') |
| 84 | + submit_button.style.display = "inline-block"; |
| 85 | + start_button.style.display = "none"; |
| 86 | + start_fetching_meet(); |
| 87 | + }else toggleSpeechRecognition(); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +submit_button.addEventListener("click", function(e){ |
| 92 | + e.preventDefault(); |
| 93 | + if(!is_google_meet)toggleSpeechRecognition(); |
| 94 | + else{ |
| 95 | + recognizing = false; |
| 96 | + submit_button.style.display = 'none'; |
| 97 | + start_button.style.display = 'inline-block'; |
| 98 | + } |
| 99 | + const req = new XMLHttpRequest(); |
| 100 | + const baseUrl = BASE_URL_VS + "/text/1"; |
| 101 | + const subject_name = document.getElementById('subject_name').value; |
| 102 | + const lecture_name = document.getElementById('lecture_name').value; |
| 103 | + const num_lines = document.getElementById('num_lines').value; |
| 104 | + if(num_lines == null)num_lines = -1; |
| 105 | + // const transcript = document.getElementById('transcript').value; |
| 106 | + // const transcript_data = localStorage.getItem('current_session'); |
| 107 | + if(is_google_meet){ |
| 108 | + catch_meet = false; |
| 109 | + transcript_data = google_meet_transcript_data; |
| 110 | + } |
| 111 | + const urlParams = { |
| 112 | + "subject_name":subject_name, |
| 113 | + "lecture_name":lecture_name, |
| 114 | + "transcript":transcript_data, |
| 115 | + "email":(JSON.parse(localStorage.getItem('user')) && JSON.parse(localStorage.getItem('user')).email ) || TEMP_EMAIL, |
| 116 | + "images":JSON.parse(localStorage.getItem('images')), |
| 117 | + "is_gmeet":is_google_meet, |
| 118 | + "num_lines":num_lines |
| 119 | + }; |
| 120 | + console.log(urlParams); |
| 121 | + req.open("POST", baseUrl); |
| 122 | + req.setRequestHeader("Content-Type", "application/json"); |
| 123 | + req.send(JSON.stringify(urlParams)); |
| 124 | + |
| 125 | + req.onreadystatechange = function() { // Call a function when the state changes. |
| 126 | + if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { |
| 127 | + console.log("Got response 200!"); |
| 128 | + transcript_data = []; |
| 129 | + localStorage.setItem('images',null); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + is_google_meet = false; |
| 134 | +}); |
| 135 | + |
| 136 | +var recognizing = false; |
| 137 | +var recognition = new webkitSpeechRecognition(); |
| 138 | +var transcript_data = []; |
| 139 | +var start_time; |
| 140 | +var end_time; |
| 141 | +recognition.continuous = true; |
| 142 | +recognition.interimResults = true; |
| 143 | + |
| 144 | +recognition.onstart = function(){ |
| 145 | + recognizing = true; |
| 146 | + recordSession(); |
| 147 | +} |
| 148 | + |
| 149 | +recognition.onerror = function(event) { |
| 150 | + console.log("Recognition error: ", event.message ? event.message : event.error); |
| 151 | + if(event.error == 'not-allowed'){ |
| 152 | + window.open('chrome-extension://'+CHROME_EXTENSION_ID+'/perms.html') |
| 153 | + } |
| 154 | + if ((event.error == "no-speech") || (event.error == "audio-capture") || (event.error == "network") || (event.error == "bad-grammar")){ |
| 155 | + refresh(); |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +recognition.onend = function() { |
| 160 | + recognizing = false; |
| 161 | +}; |
| 162 | + |
| 163 | +recognition.onresult = function(event) { |
| 164 | + for (var i = event.resultIndex; i < event.results.length; ++i) { |
| 165 | + if(event.results[i][0].confidence > 0.1) { |
| 166 | + if (event.results[i].isFinal) { |
| 167 | + console.log( event.results); |
| 168 | + var text = event.results[i][0].transcript; |
| 169 | + addToTranscript('current_session', text); |
| 170 | + var res = text.split(" "); |
| 171 | + for(let i=0;i<res.length;i++){ |
| 172 | + if(res[i] == "take"){ |
| 173 | + if((i!=res.length-1) && (res[i+1]=="snapshot")){ |
| 174 | + takeScreenShot(); |
| 175 | + acknowledgeSSSuccess(); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +}; |
| 183 | + |
| 184 | + |
| 185 | +function toggleSpeechRecognition(event) { |
| 186 | + if(recognizing) { |
| 187 | + recognition.stop(); |
| 188 | + submit_button.style.display = "none"; |
| 189 | + start_button.style.display = "inline-block"; |
| 190 | + return; |
| 191 | + } else { |
| 192 | + submit_button.style.display = "inline-block"; |
| 193 | + start_button.style.display = "none"; |
| 194 | + recognition.start(); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +function recordSession(){ |
| 199 | + createSession(); |
| 200 | + |
| 201 | + beginTime = new Date(); |
| 202 | + |
| 203 | + window.location.hash = currentSession.name; |
| 204 | +} |
| 205 | + |
| 206 | +function createSession(){ |
| 207 | + var id, name, startTime; |
| 208 | + name = 'new_session'; |
| 209 | + startTime = new Date(); |
| 210 | + currentSession = {'id': id, 'name': name, 'startTime': startTime}; |
| 211 | + // saveToLocalStorage('current_session', currentSession); |
| 212 | +} |
| 213 | + |
| 214 | +function addToTranscript(sessionName, text){ |
| 215 | + if (text){ |
| 216 | + var endTime = new Date(); |
| 217 | + transcript_data.push({"startTime": beginTime, "endTime": endTime, "text": text}); |
| 218 | + // Reset the beginTime |
| 219 | + beginTime = endTime; |
| 220 | + console.log(transcript_data) |
| 221 | + // saveToLocalStorage(sessionName, JSON.stringify(transcript)); |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +function saveToLocalStorage(key, data) { |
| 226 | + if (data && localStorage) { |
| 227 | + if (typeof data === "object"){ |
| 228 | + localStorage.setItem(key, JSON.stringify(data)); |
| 229 | + }else{ |
| 230 | + localStorage.setItem(key, data); |
| 231 | + } |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +function refresh(event) { |
| 236 | + recognizing = false; |
| 237 | + recognition.abort(); |
| 238 | + try { |
| 239 | + recognition.start() |
| 240 | + }catch(e){ |
| 241 | + |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +function acknowledgeSSSuccess(){ |
| 246 | + ss.style.backgroundColor = "green"; |
| 247 | + setTimeout(3000,()=>{ |
| 248 | + ss.style.backgroundColor = null; |
| 249 | + }) |
| 250 | +} |
| 251 | + |
| 252 | + |
| 253 | + |
0 commit comments