From ee635e017e56b2bbc64fed2b34dd496201702563 Mon Sep 17 00:00:00 2001 From: Christian Schiffler Date: Thu, 19 Sep 2019 03:15:52 +0200 Subject: [PATCH] Initialize audio buffer with button This is needed as Chrome now blocks audio playback when no gesture has been made by the user before playing the audio. So we now have a "Start game" button to fix this. --- src/guessnum.es6 | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/guessnum.es6 b/src/guessnum.es6 index b7f5f78..1019078 100644 --- a/src/guessnum.es6 +++ b/src/guessnum.es6 @@ -48,32 +48,45 @@ function numberToPhonemes(number) { */ function GuessNum(e) { const output = e.ownerDocument.createElement('pre'); + const button = e.ownerDocument.createElement('button'); const input = e.ownerDocument.createElement('input'); + const show = (e) => e.style.display = 'inline-block'; + const hide = (e) => e.style.display = 'none'; + let number e.appendChild(output); + e.appendChild(button); e.appendChild(input); + hide(input); + button.type='button'; + button.innerText = 'Start game'; + button.addEventListener('click', function() { + output.textContent = ''; + number = Math.floor((Math.random() * 99) + 1); + say(GUESS_A_NUMBER_BETWEEN_0_AND_ONE_HUNDRED); + hide(button); + show(input); + }); function say(phonemes, raw) { let text = phonemes; while (text.length < 256) { text += ' ' } - Renderer(phonemes, {phonetic: true}); if (raw) { output.innerText += "\n" + raw; } + Renderer(phonemes, {phonetic: true}); } input.onkeydown = (e) => { if (e.keyCode === 13) { e.preventDefault(); if (guess(parseInt(input.value))) { - number = Math.floor((Math.random() * 99) + 1); output.innerText = "\n" + output.innerText.split("\n").pop(); + hide(input); + show(button); } input.value = ''; } }; - output.textContent = ''; - let number = Math.floor((Math.random() * 99) + 1); - say(GUESS_A_NUMBER_BETWEEN_0_AND_ONE_HUNDRED); /** * Guess the number.