fictioneer/js/tts.min.js

1 line
6.1 KiB
JavaScript
Raw Permalink Normal View History

2024-12-21 23:33:22 +01:00
const fcn_ttsInterface=_$$$("tts-interface");var fcn_utter,fcn_synth,fcn_ttsStack=[],fcn_currentReadingId=-1,fcn_ttsPauseTimestamp=-1,fcn_ttsCurrentText="",fcn_ttsSettings=fcn_getTTSsettings(),fcn_voices=[];if("undefined"!=typeof speechSynthesis&&fcn_ttsInterface){fcn_synth=window.speechSynthesis,(fcn_utter=new SpeechSynthesisUtterance).lang=document.documentElement.lang;const t=setTimeout((()=>{fcn_setupTTS()}),2e3);"onvoiceschanged"in speechSynthesis&&fcn_synth.addEventListener("voiceschanged",(()=>{fcn_setupTTS(),clearTimeout(t)}),{once:!0})}function fcn_setupTTS(){fcn_voices.length>0||(fcn_setUpVoices(),fcn_updateVolume(fcn_ttsSettings.volume),fcn_updatePitch(fcn_ttsSettings.pitch),fcn_updateRate(fcn_ttsSettings.rate))}function fcn_setTTSsettings(t){fcn_ttsSettings=t,localStorage.setItem("ttsSettings",JSON.stringify(t))}function fcn_getTTSsettings(){const t=FcnUtils.parseJSON(localStorage.getItem("ttsSettings"))??{};return 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} (${c.lang})`,e.appendChild(a),n++}fcn_voices.length<1?_$(".tts-settings__voice-selection")?.remove():(e.addEventListener("change",(t=>{fcn_updateVoice(t.currentTarget.value)})),fcn_updateVoice(fcn_ttsSettings.voice))}function fcn_updateVoice(t){if(isNaN(t)||void 0===fcn_voices[t]){let e=fcn_voices.findIndex((t=>"Samantha"===t.name&&("en-US"===t.lang||"en_US"===t.lang)));e<0&&(e=fcn_voices.findIndex((t=>"en-US"===t.lang||"en_US"===t.lang))),t=e>-1?e:0}fcn_utter.voice=fcn_voices[t],_$$$("tts-voice-select").value=t,fcn_ttsSettings.voice=t,fcn_setTTSsettings(fcn_ttsSettings)}function fcn_updateVolume(t){t=isNaN(t)?100:parseInt(t),t=FcnUtils.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=FcnUtils.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=FcnUtils.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_ttsInterface&&(_$$$("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();const c=new Set(["P","H1","H2","H3","H4","H5","H6"]),a=["skip-tts","inside-epub",e];let i=t.target.closest("p[data-paragraph-id]");for(fcn_ttsStack.push(i);i=i.nextElementSibling;)c.has(i.tagName)&&!a.some((t=>i.classList.contains(t)))&&fcn_ttsStack.push(i);fcn_ttsStack=fcn_ttsStack.flatMap((t=>{const e=[];return FcnUtils.extractTextNodes(t).replace(s,"$1|").split("|").forEach((n=>{const s=n.trim();s.length>0&&e.push([t.id,s])})),e})),fcn_readTextStack(),doc