Skip to content

Instantly share code, notes, and snippets.

@freehuntx
Created December 14, 2020 18:09
Show Gist options
  • Save freehuntx/d5285bfb3bdd1a0a9e1f43a7105a6758 to your computer and use it in GitHub Desktop.
Save freehuntx/d5285bfb3bdd1a0a9e1f43a7105a6758 to your computer and use it in GitHub Desktop.
(() => {
if (!window.speechSynthesis || !window.SpeechSynthesisUtterance) {
return alert('Nicht unterstützt!')
}
const say = text => {
if (!text) return
window.speechSynthesis.cancel()
const msg = new SpeechSynthesisUtterance(text)
msg.lang = 'de'
msg.rate = 2.5
window.speechSynthesis.speak(msg)
}
let lastText = ''
setInterval(() => {
const segments = document.querySelectorAll('.ytp-caption-window-bottom .ytp-caption-segment')
const lastSegment = segments[segments.length-1]
if (!lastSegment) return
const text = lastSegment.innerText
if (text === lastText) return
else if (text.length < lastText.length) lastText = ''
const newText = text.slice(lastText.length).trim()
lastText = text
say(newText)
}, 10)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment