对fillword.js进行了重构

Bug407-JinHaoLin
AAAzsf 2022-06-13 16:33:03 +08:00
parent 5a62204864
commit 72e23f4e13
1 changed files with 19 additions and 13 deletions

View File

@ -1,30 +1,36 @@
isRead = true;
isChoose = true;
var reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停
var isRead = true;
var isChoose = true;
const reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停
//获取字符
function getWord(){
var word = window.getSelection?window.getSelection():document.selection.createRange().text;
return word;
return window.getSelection?window.getSelection():document.selection.createRange().text;
}
function fillinWord(){
var word = getWord();
//填充字符
function fillingWord(){
let word = getWord();
if (isRead) read(word);
if (!isChoose) return;
var element = document.getElementById("selected-words");
let element = document.getElementById("selected-words");
element.value = element.value + " " + word;
}
document.getElementById("text-content").addEventListener("click", fillinWord, false);
function read(s){
document.getElementById("text-content").addEventListener("click", fillingWord, false);
//朗读单词
//@word 要朗读的单词
function read(word){
//关闭当前正在读的单词
reader.cancel()
var msg = new SpeechSynthesisUtterance(s);
//创建新的朗读任务
let msg = new SpeechSynthesisUtterance(word);
reader.speak(msg);
}
//关闭正在读的单词
function onReadClick(){
isRead = !isRead;
let isRead = !isRead;
if(!isRead){
reader.cancel();
}
}
//取消当前选择
function onChooseClick(){
isChoose = !isChoose;
}