-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (34 loc) · 1.27 KB
/
index.js
File metadata and controls
53 lines (34 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const texts = document.getElementById('convert-text');
const response = document.getElementById('response');
const selectTag = document.querySelectorAll('select');
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new window.SpeechRecognition();
recognition.interimResults = true;
recognition.onresult = function(event) {
texts.value = event.results[0][0].transcript;
}
recognition.addEventListener('end', ()=> {
//run the gpt function with texts.value
if(texts.value != "") {
translate()
}
recognition.start();
})
recognition.start();
function translate() {
let text = texts.value
let translateFrom = selectTag[0].value
let translateTo = selectTag[1].value
if(!text) return;
response.setAttribute("placeholder", "Translating...")
let apiUrl = `https://api.mymemory.translated.net/get?q=${text}&langpair=${translateFrom}|${translateTo}`;
fetch(apiUrl).then(res => res.json()).then(data => {
response.value = data.responseData.translatedText;
data.matches.forEach(data => {
if(data.id === 0) {
response.value = data.translation;
}
})
response.setAttribute("placeholder", "Translation");
})
}