diff --git a/assets/js/scripts.js b/assets/js/scripts.js deleted file mode 100644 index a26c6f9..0000000 --- a/assets/js/scripts.js +++ /dev/null @@ -1,29 +0,0 @@ -"use strict"; - -let synth = window.speechSynthesis; - -function initSpeak() { - if (synth && document.querySelector('.content')) { - document.querySelector('#speakBtn').style.display = ''; - stopSpeak(); - } -} - -function speak() { - stopSpeak(); - let voices = synth.getVoices().filter(voice => voice.lang.startsWith(document.querySelector('html').lang)); - if (voices.length == 0) return; - document.querySelector('#speakBtn').innerHTML = "Stop speaking!"; - let utterThis = new SpeechSynthesisUtterance(document.querySelector('.content').textContent); - utterThis.voice = voices[0]; - utterThis.onerror = stopSpeak; - utterThis.onend = stopSpeak; - synth.speak(utterThis); -} - -function stopSpeak() { - synth.cancel(); - document.querySelector('#speakBtn').innerHTML = "Read to me, please."; -} - -window.onload = initSpeak(); \ No newline at end of file diff --git a/assets/js/speak.js b/assets/js/speak.js new file mode 100644 index 0000000..9503fc9 --- /dev/null +++ b/assets/js/speak.js @@ -0,0 +1,35 @@ +"use strict"; + +let synth = window.speechSynthesis; +let voice; +if (synth) { + voice = synth.getVoices().filter(voice => voice.lang.startsWith(document.querySelector('html').lang))[0]; +} + +function initSpeak() { + if (voice) { + let speakBtn = document.querySelector('#speakBtn'); + speakBtn.style.display = ''; + speakBtn.innerHTML = "Read to me, please."; + } +} + +function speak() { + console.log("Start speaking") + document.querySelector('#speakBtn').innerHTML = "Stop speaking!"; + let textContent = document.querySelector('.content').innerText; + let utterThis = new SpeechSynthesisUtterance(textContent); + utterThis.voice = voice; + utterThis.onerror = stopSpeak; + utterThis.onend = stopSpeak; + synth.speak(utterThis); +} + +function stopSpeak() { + console.log("Stop speaking") + synth.cancel(); + document.querySelector('#speakBtn').innerHTML = "Read to me, please."; +} + +initSpeak(); +window.onbeforeunload = function () { stopSpeak(); } \ No newline at end of file diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index abd2c63..6573cc6 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -41,7 +41,5 @@ {{ partialCached "footer" . }} {{ partialCached "custombodyend" . }} - {{ $scripts := resources.Get "js/scripts.js" | minify | fingerprint }} - \ No newline at end of file diff --git a/layouts/_default/single.html b/layouts/_default/single.html index e951cf9..2e0206c 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -6,9 +6,14 @@

{{ . }}

{{ end }} {{ if not .Params.nometa }} - {{ with partialCached "post_meta" . .RelPermalink }} -
{{ . }}
- {{ end }} +
+ {{ partialCached "post_meta" . .RelPermalink }} + {{ if (.Param "speak") }} + + {{ $speakScript := resources.Get "js/speak.js" | minify | fingerprint }} + + {{ end }} +
{{ end }} {{ partial "oldcontentwarning" . }} diff --git a/layouts/partials/post_meta.html b/layouts/partials/post_meta.html index 5957413..ba1e845 100644 --- a/layouts/partials/post_meta.html +++ b/layouts/partials/post_meta.html @@ -37,7 +37,6 @@ {{ T "also_on" }}:{{ $delimiter := "" }}{{ range $index, $link := . }}{{ $delimiter }} {{ (urls.Parse $link).Host }}{{ $delimiter = "," }}{{ end }} {{ end }} - {{ if .IsTranslated }}
{{ i18n "translations" }}: {{ $delimiter := "" }}{{ range .Translations }}{{ $delimiter }}{{ .Language.LanguageName }}{{ $delimiter = ", " }}{{ end }}