const audio_btns = document.querySelectorAll("[name='audio_btn']");
const audio_input = document.querySelector("[name='audio_input']");
const sounds = [];
document.addEventListener('input', function(inp) {
const { target } = inp;
if (target.name === "audio_input") {
if (!target.value.match(/^[a-zA-Z],?((,[a-zA-Z],?)?)*$/)) {
target.value = target.value.slice(0, -1);
}
target.value = target.value.toUpperCase();
}
});
audio_input.addEventListener("keypress", (event) => {
const { key } = event;
if (key === "Enter") {
if (sounds.length > 0) return;
const values = audio_input.value.split(",");
values.forEach((value) => {
Array.from(audio_btns).find((btn) => {
if (btn.textContent.trim() === value) {
sounds.push(btn.children[0]);
}
})
});
playSounds(sounds);
}
});
document.addEventListener('click', async (event) => {
const { target } = event;
if (target.name === "audio_btn") {
const audio = target.children[0];
await audio.play();
}
})
const playSounds = (sounds) => {
if (sounds.length > 0) {
const sound = sounds[0];
sound.play();
sound.addEventListener('ended', () => {
sounds.shift();
if (sounds.length > 0) {
playSounds(sounds);
}
}, {once: true});
}
}