Created
April 27, 2020 06:45
-
-
Save hitode909/2c325ff256e185c7a831a33e52993928 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
const speak = (body, isRetry) => { | |
window.speechSynthesis.cancel() | |
if (!body) return; | |
console.log('speak ' + body); | |
const synth = window.speechSynthesis; | |
const allVoices = synth.getVoices(); | |
const jaVoices = allVoices.filter(v => v.lang === 'ja-JP'); | |
const voice = jaVoices[0]; | |
if (!voice && !isRetry) { | |
speak(body, true) | |
} | |
const utter = new SpeechSynthesisUtterance(body); | |
utter.voice = voice; | |
utter.lang = 'ja-JP'; | |
utter.pitch = 1 | |
utter.volume = 1 | |
utter.rate = 1 | |
synth.speak(utter) | |
}; | |
const listen = () => { | |
new MutationObserver((mutations, observer) => { | |
let phrases = [] | |
for (const m of mutations) { | |
if (m.type !== 'childList') { | |
continue | |
} | |
phrases.push(...Array.from(m.addedNodes).map(n => n.textContent).filter(s => s.length > 0)) | |
} | |
console.log(phrases) | |
if (phrases.length > 0) { | |
speak(phrases.join("\n")) | |
} | |
}).observe(document.querySelector('.p-message_pane'), { | |
childList: true, | |
subtree: true, | |
}) | |
}; | |
listen(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment