2023-04-25 15:51:39 +08:00
|
|
|
var isRead = true;
|
|
|
|
var isChoose = true;
|
|
|
|
const reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停
|
|
|
|
//获取字符
|
2022-07-18 16:00:08 +08:00
|
|
|
function getWord() {
|
2023-04-25 15:51:39 +08:00
|
|
|
return window.getSelection
|
|
|
|
? window.getSelection()
|
|
|
|
: document.selection.createRange().text;
|
2022-01-26 21:10:09 +08:00
|
|
|
}
|
2023-04-25 15:51:39 +08:00
|
|
|
//填充字符
|
|
|
|
function fillingWord() {
|
|
|
|
let word = getWord();
|
|
|
|
if (isRead) read(word);
|
|
|
|
if (!isChoose) return;
|
|
|
|
let element = document.getElementById("selected-words");
|
|
|
|
element.value = element.value + " " + word;
|
2022-07-18 16:00:08 +08:00
|
|
|
}
|
2023-04-25 15:51:39 +08:00
|
|
|
document
|
|
|
|
.getElementById("text-content")
|
|
|
|
.addEventListener("click", fillingWord, false);
|
|
|
|
//朗读单词
|
|
|
|
//@word 要朗读的单词
|
|
|
|
function read(word) {
|
|
|
|
//关闭当前正在读的单词
|
|
|
|
reader.cancel();
|
|
|
|
//创建新的朗读任务
|
|
|
|
let msg = new SpeechSynthesisUtterance(word);
|
|
|
|
reader.speak(msg);
|
2022-01-26 21:10:09 +08:00
|
|
|
}
|
2023-04-25 15:51:39 +08:00
|
|
|
//关闭正在读的单词
|
2022-07-18 16:00:08 +08:00
|
|
|
function onReadClick() {
|
2023-04-25 15:51:39 +08:00
|
|
|
let isRead = !isRead;
|
|
|
|
if (!isRead) {
|
|
|
|
reader.cancel();
|
|
|
|
}
|
2022-01-26 21:10:09 +08:00
|
|
|
}
|
2023-04-25 15:51:39 +08:00
|
|
|
//取消当前选择
|
2022-07-18 16:00:08 +08:00
|
|
|
function onChooseClick() {
|
2023-04-25 15:51:39 +08:00
|
|
|
isChoose = !isChoose;
|
2022-07-18 16:00:08 +08:00
|
|
|
}
|