Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created April 27, 2020 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitode909/2c325ff256e185c7a831a33e52993928 to your computer and use it in GitHub Desktop.
Save hitode909/2c325ff256e185c7a831a33e52993928 to your computer and use it in GitHub Desktop.
(() => {
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