ColaCoca's picture
jeju ๋ชจ๋ธ ์—…๋กœ๋“œ
8eb2cb0
// ํŽ˜์ด์ง€๊ฐ€ ๋ Œ๋”๋ง ๋ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ์‹œ์ž‘
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);
}
});