File size: 1,880 Bytes
04f98c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
document.addEventListener('DOMContentLoaded', () => {
    document.getElementById('startRecording').addEventListener('click', startRecording);
    document.getElementById('stopRecording').addEventListener('click', stopRecording);
    document.getElementById('speakTranslation').addEventListener('click', speakTranslation);

    fetchSupportedLanguages();
});

async function startRecording() {
    const response = await fetch('/start-recording', {
        method: 'POST'
    });
    const result = await response.text();
    document.getElementById('status').innerText = result;
}

async function stopRecording() {
    const targetLanguage = document.getElementById('targetLanguage').value;
    const response = await fetch('/stop-recording', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ targetLanguage })
    });
    const result = await response.json();
    document.getElementById('recognizedText').innerText = result.recognizedText;
    document.getElementById('translatedText').innerText = result.translatedText;
}

async function speakTranslation() {
    const response = await fetch('/speak-translation', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({})
    });
    const result = await response.text();
    document.getElementById('status').innerText = result;
}

async function fetchSupportedLanguages() {
    const response = await fetch('/get-supported-languages');
    const languages = await response.json();
    const select = document.getElementById('targetLanguage');
    for (const [key, value] of Object.entries(languages)) {
        const option = document.createElement('option');
        option.value = key;
        option.text = value;
        select.add(option);
    }
}