1 line
6.0 KiB
JavaScript
1 line
6.0 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(){const t=fcn_synth.getVoices(),e=_$$$("tts-voice-select");if(!e)return;let n=0;for(let s=0;s<t.length;s++){const c=t[s];fcn_voices.push(c);const a=document.createElement("option");a.value=n,a.innerHTML=c.name,e.appendChild(a),n++}fcn_voices.length<1?_$(".tts-interface__voice-selection").remove():(e.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,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,_$$$("tts-volume-reset").classList.toggle("_modified",100!=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,_$$$("tts-pitch-reset").classList.toggle("_modified",1!=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,_$$$("tts-rate-reset").classList.toggle("_modified",1!=t),fcn_ttsSettings.rate=t,fcn_setTTSsettings(fcn_ttsSettings),fcn_utter.rate=t}function fcn_readTextStack(){const 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="");const 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;const e=_$(".chapter-formatting")?.classList.contains("hide-sensitive")??!1?"sensitive-content":"sensitive-alternative",n=_$$$("button-tts-play"),s=new RegExp(fcn_ttsInterface.dataset.regex,"gm");fcn_synth.speaking&&fcn_utter.removeEventListener("end",fcn_readTextStack),fcn_synth.cancel();let c=t.target.closest("p[data-paragraph-id]");for(fcn_ttsStack.push(c);c=c.nextElementSibling;)!fcn_ttsAllowedTags.includes(c.tagName)||c.classList.contains("skip-tts")||c.classList.contains("inside-epub")||c.classList.contains(e)||fcn_ttsStack.push(c);fcn_ttsStack=fcn_ttsStack.flatMap((t=>{const e=[],n=t.querySelector(".paragraph-inner");return(n?n.textContent:t.textContent).replace(s,"$1|").split("|").forEach((n=>{const s=n.trim();s.length>0&&e.push([t.id,s])})),e})),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=>{const 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",(()=>{fcn_scrollTo(_$(`p[id="${fcn_currentReadingId}"]`),128)})),_$$("#tts-volume-range, #tts-volume-text").forEach((t=>{t.addEventListener("input",(t=>{fcn_updateVolume(t.target.value)}))})),_$$$("tts-volume-reset")?.addEventListener("click",(()=>{fcn_updateVolume(100)})),_$$("#tts-pitch-range, #tts-pitch-text").forEach((t=>{t.addEventListener("input",(t=>{fcn_updatePitch(t.target.value)}))})),_$$$("tts-pitch-reset")?.addEventListener("click",(()=>{fcn_updatePitch(1)})),_$$("#tts-rate-range, #tts-rate-text").forEach((t=>{t.addEventListener("input",(t=>{fcn_updateRate(t.target.value)}))})),_$$$("tts-rate-reset")?.addEventListener("click",(()=>{fcn_updateRate(1)})),window.addEventListener("beforeunload",(()=>{fcn_synth.cancel()}))); |