/** * Function called when the user clicks the submit button or presses Enter. */ async function submit() { // Get the original and translated words, send them to the server. original = document.getElementById("original").innerHTML; translation = document.getElementById("translation").value; uri = `/submit/${original}/${translation}`; let response = await fetch(uri); let response_text = await response.text(); if (!response.ok) { // something unexpected happened... console.log(`Error: ${response_text} in response to '${uri}'`); return; } // Update the status text. document.getElementById("reply").innerText = response_text; // And fetch the next word. next(); } /** * Update the stats. */ async function updateStats() { uri = '/stats'; let response = await fetch(uri); let response_text = await response.text(); document.getElementById("stats").innerText = response_text; } /** * Fetches a new word to translate. Called automatically after submit(), and initially for the first * word. */ async function next() { // Ensure the stats are correctly displayed. updateStats(); uri = `/random`; let response = await fetch(uri); let response_text = await response.text(); if (!response.ok) { console.log(`Error: ${response_text} in response to '${uri}'`); return; } document.getElementById("original").innerText = response_text; document.getElementById("translation").value = ""; } /** Called when a key is released within input. */ async function keyup(input, event) { var code = event.code; if (code == "Enter") { submit(); } } // Initial call to fetch the first word. next();