File size: 1,778 Bytes
8eb2cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ํŽ˜์ด์ง€๊ฐ€ ๋ Œ๋”๋ง ๋ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ์‹œ์ž‘
window.addEventListener('DOMContentLoaded', function()
{
    let voiceForm = document.getElementById("voiceForm");

    // ํผ ์ „์†ก์‹œ ์‹คํ–‰
    voiceForm.addEventListener("submit", (e) => {
        e.preventDefault();

        let sentence = document.getElementById("sentence");

        if (sentence == "") {
            alert("๋ฌธ์žฅ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!");
        } else {
            // perform operation with form input
            alert(sentence.value + " ๋ฌธ์žฅ์ด ์ž…๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!");
            console.log("getPost() ํ•จ์ˆ˜ ํ˜ธ์ถœ")
            getVoice(sentence.value)
            // location.href='/voice/'+sentence.value;
        //      `This form has a username of ${username.value} and password of ${password.value}`

        }
    });
    
    function getVoice(sentence){
        $.ajax({
            type:"get",  // fetch์˜ method ๊ธฐ๋Šฅ
            url: "/voice/"+sentence, 
            timeout:10000,
            // ์„ฑ๊ณต
            success:function(audio_src){
                console.log("success " + audio_src);
                print_voice(audio_src);
            },
            error:function(request,error){
                alert("fail " + sentence);
            }
        })
    }
    
    
    // ์Œ์„ฑํŒŒ์ผ์„ ํ™”๋ฉด์— ์ถœ๋ ฅ
    // ์Œ์„ฑ ์ถœ๋ ฅ ์—ฌ๋ถ€ ๋ฒ„ํŠผ์— ๋”ฐ๋ผ audio ํƒœ๊ทธ ์†์„ฑ autoplay ๊ฐ€ ๋ณ€๊ฒฝ๋˜์•ผ ํ•จ
    function print_voice(audio_src){
        let voice_tag = document.querySelector("#voice");
        voice_tag_html = `<h1>๋ฌธ์žฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค~ ${sentence.value} </h1><br>
                <audio src="../${audio_src}" controls autoplay></audio>`;
        voice_tag.insertAdjacentHTML("beforeend", voice_tag_html);
    }
    
    
    
    
});