From 5e714802f05e9e3ecb2abc59c9c4dd88090c36c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bankn8II=C2=A9=24A?= <102619282+barionleg@users.noreply.github.com> Date: Thu, 21 Dec 2023 11:23:30 +0100 Subject: [PATCH] Create script.js --- tokipona/script.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tokipona/script.js diff --git a/tokipona/script.js b/tokipona/script.js new file mode 100644 index 0000000..da91a33 --- /dev/null +++ b/tokipona/script.js @@ -0,0 +1,65 @@ +onload = function() { + if ('speechSynthesis' in window) with(speechSynthesis) { + + + var playEle = document.querySelector('#play'); + var pauseEle = document.querySelector('#pause'); + var stopEle = document.querySelector('#stop'); + var flag = false; + + + playEle.addEventListener('click', onClickPlay); + pauseEle.addEventListener('click', onClickPause); + stopEle.addEventListener('click', onClickStop); + + function onClickPlay() { + if(!flag){ + flag = true; + utterance = new SpeechSynthesisUtterance(document.querySelector('article').textContent); + utterance.voice = getVoices()[0]; + utterance.onend = function(){ + flag = false; playEle.className = pauseEle.className = ''; stopEle.className = 'stopped'; + }; + playEle.className = 'played'; + stopEle.className = ''; + speak(utterance); + } + if (paused) { /* unpause/resume narration */ + playEle.className = 'played'; + pauseEle.className = ''; + resume(); + } + } + + function onClickPause() { + if(speaking && !paused){ /* pause narration */ + pauseEle.className = 'paused'; + playEle.className = ''; + pause(); + } + } + + function onClickStop() { + if(speaking){ /* stop narration */ + /* for safari */ + stopEle.className = 'stopped'; + playEle.className = pauseEle.className = ''; + flag = false; + cancel(); + + } + } + + } + + else { /* speech synthesis not supported */ + msg = document.createElement('h5'); + msg.textContent = "Detected no support for Speech Synthesis"; + msg.style.textAlign = 'center'; + msg.style.backgroundColor = 'red'; + msg.style.color = 'white'; + msg.style.marginTop = msg.style.marginBottom = 0; + document.body.insertBefore(msg, document.querySelector('div')); + } + +}