// === Voice Quiz Enhancer for Watu Pro ===
// Adds TTS (reads the question) and STT (speech input for answers)
document.addEventListener("DOMContentLoaded", () => {
const speak = (text) => {
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = 'en-US';
speechSynthesis.speak(utterance);
};
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.lang = 'en-US';
recognition.continuous = false;
recognition.interimResults = false;
// Speak question + choices
const questionBlock = document.querySelector(".watu-question");
if (questionBlock) {
const textToSpeak = questionBlock.innerText.replace(/\s+/g, " ");
speak(textToSpeak);
}
// Listen for spoken answers
recognition.onresult = function (event) {
const transcript = event.results[0][0].transcript.toLowerCase();
let selected = false;
['a', 'b', 'c', 'd'].forEach(letter => {
if (transcript.includes(letter) && !selected) {
const label = document.querySelector(`label[for*=answer_${letter}]`);
if (label) {
label.click(); // Simulate clicking the label (selects answer)
selected = true;
speak(`You selected option ${letter.toUpperCase()}`);
const submitBtn = document.querySelector("input[type='submit']");
if (submitBtn) {
setTimeout(() => submitBtn.click(), 1000); // Auto-submit
}
}
}
});
if (!selected) speak("Sorry, I didn't catch that. Please say A, B, C, or D.");
};
// Start recognition on button click
const listenBtn = document.createElement("button");
listenBtn.textContent = "🎤 Answer by Voice";
listenBtn.style.marginTop = "10px";
listenBtn.onclick = () => {
recognition.start();
speak("Listening for your answer.");
};
const container = document.querySelector(".watu-question")?.parentElement;
if (container) container.appendChild(listenBtn);
});