File size: 2,035 Bytes
e12a899
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
// Variable definitions
const app = document.getElementById('app');
const audioColumn = document.getElementById('audio-column');
const textColumn = document.getElementById('text-column');
const audioControl = document.getElementById('audio');
const transcript = document.getElementById('text');
const submitButton = document.getElementById('submit');
const loader = document.getElementById('loader');
const backButton = document.getElementById('backButton');
const form = document.getElementById('form');
const fileExtention = document.getElementById('fileExtention');
const backButtonLoader = document.getElementById('backButtonLoader');
const transcribe = async (event) => {
    event.preventDefault();
    if (!fileExtention.value) {
        alert('Please specify the file extension first!');
        return false;
    }
    loader.classList.remove('hidden');
    submitButton.disabled = true;
    try {
        let response = await fetch('/transcribe', {
            method: 'POST',
            body: JSON.stringify({
                fileExt: fileExtention.value,
                tranFile: event.target.file.value
            })
        });
        if (response.status === 200) {
            let data = await response.json();
            transcript.textContent = data.text;
            audioControl.src = data.audioUrl;
            audioControl.playsinline = true;
            audioControl.addEventListener("loadeddata", () => {
                audioControl.play();
            });
            submitButton.disabled = false;
            loader.classList.add('hidden');
        } else {
            console.log(response);
            alert('Invalid file!');
        }
    } catch (err) {
        console.log(err);
        alert('An error occurred while uploading the file! Please try again');
    }
};

// Event listeners
form.addEventListener('submit', transcribe);
backButton.addEventListener('click', () => {
    backButtonLoader.classList.remove('hidden');
    setTimeout(() => {
        window.history.back();
    }, 1500);
});