2023-05-25 17:35:31 +08:00
|
|
|
var Reader = (function() {
|
|
|
|
let reader = window.speechSynthesis;
|
|
|
|
let current_position = 0;
|
|
|
|
let original_position = 0;
|
|
|
|
let to_speak = "";
|
|
|
|
|
|
|
|
function makeUtterance(str, rate) {
|
|
|
|
let msg = new SpeechSynthesisUtterance(str);
|
|
|
|
msg.rate = rate;
|
|
|
|
msg.lang = "en-US";
|
|
|
|
msg.onboundary = ev => {
|
2023-06-02 21:33:21 +08:00
|
|
|
if (ev.name === "word") {
|
2023-05-25 17:35:31 +08:00
|
|
|
current_position = ev.charIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function read(s, rate) {
|
|
|
|
to_speak = s.toString();
|
|
|
|
original_position = 0;
|
|
|
|
current_position = 0;
|
|
|
|
let msg = makeUtterance(to_speak, rate);
|
|
|
|
reader.speak(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopRead() {
|
|
|
|
reader.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
read: read,
|
|
|
|
stopRead: stopRead
|
|
|
|
};
|
2023-06-02 21:33:21 +08:00
|
|
|
}) ();
|