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);
}
}); |