1 line
6.1 KiB
JavaScript
1 line
6.1 KiB
JavaScript
const fcn_ttsInterface=_$$$("tts-interface"),fcn_ttsAllowedTags=["P","H1","H2","H3","H4","H5","H6"];var fcn_utter,fcn_synth,fcn_ttsStack=[],fcn_currentReadingId=-1,fcn_ttsPauseTimestamp=-1,fcn_ttsCurrentText="",fcn_ttsSettings=fcn_getTTSsettings(),fcn_voices=[];function fcn_setTTSsettings(t){fcn_ttsSettings=t,localStorage.setItem("ttsSettings",JSON.stringify(t))}function fcn_getTTSsettings(){let t=localStorage.getItem("ttsSettings");return t=t&&fcn_isValidJSONString(t)?JSON.parse(t):{},fcn_setTTSsettings(t),t}function fcn_setUpVoices(){let t=fcn_synth.getVoices(),e=0,n=_$$$("tts-voice-select"),s=[fcn_theRoot.lang];if(n){for(let c=0;c<t.length;c++){let a=t[c];if("en-US"==fcn_theRoot.lang&&s.push("en-GB"),"en-GB"==fcn_theRoot.lang&&s.push("en-US"),!s.includes(a.lang))continue;fcn_voices.push(a);let i=document.createElement("option");i.value=e,i.innerHTML=a.name,n.appendChild(i),e++}fcn_voices.length<1?_$(".tts-interface__voice-selection").remove():(n.addEventListener("change",(t=>{fcn_updateVoice(t.currentTarget.value)})),fcn_updateVoice(fcn_ttsSettings.voice))}}function fcn_updateVoice(t){t=isNaN(t)?0:t;let e=fcn_voices[t];void 0===e&&(e=fcn_voices[0]),fcn_utter.voice=e,_$$$("tts-voice-select").value=t,_$$$("selected-voice-label").innerHTML=`Selected Voice: ${e.name} (${e.lang})`,fcn_ttsSettings.voice=t,fcn_setTTSsettings(fcn_ttsSettings)}function fcn_updateVolume(t){t=isNaN(t)?100:parseInt(t),t=fcn_clamp(0,100,t),_$$$("tts-volume-range").value=t,_$$$("tts-volume-text").value=t,fcn_ttsSettings.volume=t,fcn_setTTSsettings(fcn_ttsSettings),fcn_utter.volume=t/100}function fcn_updatePitch(t){t=isNaN(t)?1:parseFloat(t),t=fcn_clamp(.2,1.8,t),_$$$("tts-pitch-range").value=t,_$$$("tts-pitch-text").value=t,fcn_ttsSettings.pitch=t,fcn_setTTSsettings(fcn_ttsSettings),fcn_utter.pitch=t}function fcn_updateRate(t){t=isNaN(t)?1:parseFloat(t),t=fcn_clamp(.2,1.8,t),_$$$("tts-rate-range").value=t,_$$$("tts-rate-text").value=t,fcn_ttsSettings.rate=t,fcn_setTTSsettings(fcn_ttsSettings),fcn_utter.rate=t}function fcn_readTextStack(){let t=_$(".current-reading");if(0===fcn_ttsStack.length)return fcn_ttsInterface.classList.add("ended"),t&&t.classList.remove("current-reading"),fcn_currentReadingId=-1,void(fcn_ttsCurrentText="");let e=fcn_ttsStack.shift();fcn_ttsCurrentText=e[1],fcn_currentReadingId!=e[0]&&(fcn_currentReadingId=e[0],t&&t.classList.remove("current-reading"),_$$$(fcn_currentReadingId).classList.add("current-reading")),fcn_utter.text=fcn_ttsCurrentText,fcn_utter.addEventListener("end",fcn_readTextStack,{once:!0}),fcn_synth.speak(fcn_utter)}"undefined"!=typeof speechSynthesis&&(fcn_synth=window.speechSynthesis,(fcn_utter=new SpeechSynthesisUtterance).lang=fcn_theRoot.lang,"onvoiceschanged"in speechSynthesis?fcn_synth.addEventListener("voiceschanged",(()=>{fcn_setUpVoices(),fcn_updateVolume(fcn_ttsSettings.volume),fcn_updatePitch(fcn_ttsSettings.pitch),fcn_updateRate(fcn_ttsSettings.rate)}),{once:!0}):setTimeout((()=>{fcn_setUpVoices(),fcn_updateVolume(fcn_ttsSettings.volume),fcn_updatePitch(fcn_ttsSettings.pitch),fcn_updateRate(fcn_ttsSettings.rate)}),2e3)),"undefined"!=typeof speechSynthesis&&(_$$$("button-tts-set").addEventListener("click",(t=>{fcn_ttsStack=[],fcn_currentReadingId=-1;let e=_$(".chapter-formatting")?.classList.contains("hide-sensitive")??!1?"sensitive-content":"sensitive-alternative",n=_$$$("button-tts-play");fcn_synth.speaking&&fcn_utter.removeEventListener("end",fcn_readTextStack),fcn_synth.cancel();let s=t.target.closest("p[data-paragraph-id]");for(fcn_ttsStack.push(s);s=s.nextSibling;)1!=s.nodeType||!fcn_ttsAllowedTags.includes(s.tagName)||s.classList.contains("skip-tts")||s.classList.contains("inside-epub")||s.classList.contains(e)||fcn_ttsStack.push(s);fcn_ttsStack=fcn_ttsStack.map((t=>{let e=[],n=t.querySelector(".paragraph-inner"),s=n?n.innerText:t.innerText;return s=s.replace(/(\.+|\:|\!|\?)(\"*|\'*|\)*|}*|]*)(\s|\n|\r|\r\n)/gm,"$1$2|").split("|"),s.forEach((n=>{(n=n.trim())&&e.push([t.id,n])})),e})).flat(),fcn_readTextStack(),fcn_theBody.classList.add("tts-open"),fcn_ttsInterface.classList.remove("hidden","ended","paused"),fcn_ttsInterface.classList.add("playing"),n.focus(),n.blur()})),_$$$("button-tts-stop")?.addEventListener("click",(t=>{let e=_$(".current-reading");fcn_ttsInterface.classList.add("hidden","ended"),fcn_ttsInterface.classList.remove("playing","paused"),fcn_theBody.classList.remove("tts-open"),e&&e.classList.remove("current-reading"),fcn_ttsStack=[],fcn_currentReadingId=-1,fcn_utter.removeEventListener("end",fcn_readTextStack),fcn_synth.cancel()})),_$$$("button-tts-play")?.addEventListener("click",(t=>{fcn_synth.resume(),-1!==fcn_ttsPauseTimestamp&&Date.now()-fcn_ttsPauseTimestamp>1e4&&(fcn_ttsStack.unshift([fcn_currentReadingId,fcn_ttsCurrentText]),fcn_synth.cancel(),fcn_readTextStack(),fcn_ttsPauseTimestamp=-1),fcn_ttsInterface.classList.add("playing"),fcn_ttsInterface.classList.remove("paused")})),_$$$("button-tts-pause")?.addEventListener("click",(t=>{fcn_synth.pause(),fcn_ttsPauseTimestamp=Date.now(),fcn_ttsInterface.classList.remove("playing"),fcn_ttsInterface.classList.add("paused")})),_$$$("button-tts-skip")?.addEventListener("click",(t=>{fcn_utter.removeEventListener("end",fcn_readTextStack),fcn_synth.cancel(),fcn_readTextStack(),fcn_ttsInterface.classList.remove("paused"),fcn_ttsInterface.classList.add("playing")})),_$$$("button-tts-scroll")?.addEventListener("click",(t=>{let e=_$(`p[id="${fcn_currentReadingId}"]`).getBoundingClientRect().top+window.pageYOffset-128;window.scrollTo({top:e,behavior:"smooth"})})),_$$$("tts-volume-range")?.addEventListener("input",(t=>{fcn_updateVolume(t.target.value)})),_$$$("tts-volume-text")?.addEventListener("input",(t=>{fcn_updateVolume(t.target.value)})),_$$$("tts-pitch-range")?.addEventListener("input",(t=>{fcn_updatePitch(t.target.value)})),_$$$("tts-pitch-text")?.addEventListener("input",(t=>{fcn_updatePitch(t.target.value)})),_$$$("tts-rate-range")?.addEventListener("input",(t=>{fcn_updateRate(t.target.value)})),_$$$("tts-rate-text")?.addEventListener("input",(t=>{fcn_updateRate(t.target.value)})),_$$$("tts-settings-toggle")?.addEventListener("change",(t=>{t.currentTarget.closest("#tts-interface").dataset.showSettings=t.currentTarget.checked}))); |