Skip to content

Instantly share code, notes, and snippets.

@nacika-ins
Last active May 17, 2020 11:32
Show Gist options
  • Save nacika-ins/580b2d2508a4cee6a1a1c6f0e1b54174 to your computer and use it in GitHub Desktop.
Save nacika-ins/580b2d2508a4cee6a1a1c6f0e1b54174 to your computer and use it in GitHub Desktop.
マストドン音声読み上げ v3対応 (MITライセンス)
(function () {
const synth = window.speechSynthesis;
const home = document.querySelector('.item-list[role="feed"]')
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
const name = (mutation.addedNodes[0].querySelector(".display-name strong").textContent || "")
.replace(/@/g, "アットマーク")
;
const text = (mutation.addedNodes[0].querySelector("p").textContent || "")
.replace(/https?:\/\/[\w/:%#\$&\?\(\)~\.=\+\-]+/g, "")
.replace(/@/g, "アットマーク")
.replace(/([^a-z^A-Z])ltl([^a-z^A-Z]?)/ig, "$1。ローカルタイムライン。$2")
.replace(/([^a-z^A-Z])ftl([^a-z^A-Z]?)/ig, "$1。連合タイムライン。$2")
.replace(/([^a-z^A-Z])pawoo([^a-z^A-Z]?)/ig, "$1。ぱうー。$2")
.replace(/ /g, "。。。")
.replace(/ww.+/g, "。だいぞうきば。")
.replace(/ww/g, "。きば生える。")
.replace(/([^a-z^A-Z])w([^a-z^A-Z]?)/g, "$1。きば。$2")
.replace(/ww.+/g, "。だいぞうきば。")
.replace(/ww/g, "。きば生える。")
.replace(/([^a-z^A-Z])w([^a-z^A-Z]?)/g, "$1。きば。$2")
.replace(/jj.+/g, "。だいぞうきば。")
.replace(/jj/g, "。きば生える。")
.replace(/([^a-z^A-Z])j([^a-z^A-Z]?)/g, "$1。きば。$2")
.replace(/jj.+/g, "。だいぞうきば。")
.replace(/jj/g, "。きば生える。")
.replace(/([^a-z^A-Z])j([^a-z^A-Z]?)/g, "$1。きば。$2")
.replace(/([^a-z^A-Z])JK([^a-z^A-Z]?)/g, "$1。常識に考えて。$2")
.replace(/→/g, "は、")
;
const output = name+"。。。"+text;
console.log(output);
const ssu = new SpeechSynthesisUtterance(output);
ssu.rate = 1.2;
ssu.lang = "ja-JP";
const voices = synth.getVoices().filter( voice => voice.lang == "ja-JP" );
const voiceNumber = Math.round(Math.random() * voices.length);
ssu.voice = voices[voiceNumber];
synth.speak(ssu);
});
});
const config = { attributes: true, childList: true, characterData: true };
observer.observe(home, config);
})();
@nacika-ins
Copy link
Author

nacika-ins commented May 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment