2022-07-18 16:00:08 +08:00
|
|
|
|
let isRead = true;
|
|
|
|
|
let isChoose = true;
|
|
|
|
|
let reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停
|
|
|
|
|
let current_position = 0; // 朗读文本的当前位置
|
|
|
|
|
let original_position = 0; // 朗读文本的初始位置
|
|
|
|
|
let to_speak = ""; // 朗读的初始内容
|
2022-01-26 21:10:09 +08:00
|
|
|
|
|
2022-07-18 16:00:08 +08:00
|
|
|
|
function getWord() {
|
|
|
|
|
return window.getSelection ? window.getSelection() : document.selection.createRange().text;
|
2022-01-26 21:10:09 +08:00
|
|
|
|
}
|
2022-07-18 16:00:08 +08:00
|
|
|
|
|
|
|
|
|
function fillInWord() {
|
|
|
|
|
let word = getWord();
|
|
|
|
|
if (isRead) read(word);
|
|
|
|
|
if (!isChoose) return;
|
|
|
|
|
const element = document.getElementById("selected-words");
|
|
|
|
|
element.value = element.value + " " + word;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.getElementById("text-content").addEventListener("click", fillInWord, false);
|
|
|
|
|
|
|
|
|
|
function makeUtterance(str, rate) {
|
|
|
|
|
let msg = new SpeechSynthesisUtterance(str);
|
|
|
|
|
msg.rate = rate;
|
|
|
|
|
msg.lang = "en-US"; // TODO: add language options menu
|
|
|
|
|
msg.onboundary = ev => {
|
|
|
|
|
if (ev.name == "word") {
|
|
|
|
|
current_position = ev.charIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return msg;
|
2022-01-26 21:10:09 +08:00
|
|
|
|
}
|
2022-07-18 16:00:08 +08:00
|
|
|
|
|
|
|
|
|
const sliderValue = document.getElementById("rangeValue"); // 显示值
|
|
|
|
|
const inputSlider = document.getElementById("rangeComponent"); // 滑块元素
|
|
|
|
|
inputSlider.oninput = () => {
|
|
|
|
|
let value = inputSlider.value; // 获取滑块的值
|
|
|
|
|
sliderValue.textContent = value + '×';
|
|
|
|
|
if (!reader.speaking) return;
|
|
|
|
|
reader.cancel();
|
|
|
|
|
let msg = makeUtterance(to_speak.substring(original_position + current_position), value);
|
|
|
|
|
original_position = original_position + current_position;
|
|
|
|
|
current_position = 0;
|
|
|
|
|
reader.speak(msg);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function read(s) {
|
|
|
|
|
to_speak = s.toString();
|
|
|
|
|
original_position = 0;
|
|
|
|
|
current_position = 0;
|
|
|
|
|
let msg = makeUtterance(to_speak, inputSlider.value);
|
|
|
|
|
reader.speak(msg);
|
2022-01-26 21:10:09 +08:00
|
|
|
|
}
|
2022-07-18 16:00:08 +08:00
|
|
|
|
|
|
|
|
|
function onReadClick() {
|
2022-01-26 21:10:09 +08:00
|
|
|
|
isRead = !isRead;
|
2022-07-18 16:00:08 +08:00
|
|
|
|
if (!isRead) {
|
|
|
|
|
reader.cancel();
|
2022-01-26 21:10:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-18 16:00:08 +08:00
|
|
|
|
|
|
|
|
|
function onChooseClick() {
|
2022-01-26 21:10:09 +08:00
|
|
|
|
isChoose = !isChoose;
|
2022-07-18 16:00:08 +08:00
|
|
|
|
}
|
2022-11-25 15:42:37 +08:00
|
|
|
|
|
|
|
|
|
function stopRead() {
|
|
|
|
|
reader.cancel();
|
2023-05-06 18:18:12 +08:00
|
|
|
|
}
|
|
|
|
|
if (performance.navigation.type == 1) { //如果网页刷新,停止播放声音
|
|
|
|
|
stopRead()
|
|
|
|
|
}
|