From 8924166975aeb7517efc14e1aed5b1af1aeb8bba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A6=83=E8=89=B3=E7=BE=8E?= <3183597872@qq.com> Date: Sun, 12 Jun 2022 21:23:34 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=87=B3=20'static\js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "static\\js/fillword.js" | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "static\\js/fillword.js" diff --git "a/static\\js/fillword.js" "b/static\\js/fillword.js" new file mode 100644 index 0000000..9843331 --- /dev/null +++ "b/static\\js/fillword.js" @@ -0,0 +1,32 @@ +isRead = true; +isChoose = true; +var reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停 +reader.cancel();// 初始化时清空reader列表 +function getWord(){ + var word = window.getSelection?window.getSelection():document.selection.createRange().text; + return word; +} +function fillinWord(){ + var word = getWord(); + if (isRead) read(word); + if (!isChoose) return; + var element = document.getElementById("selected-words"); + element.value = element.value + " " + word; +} +document.getElementById("text-content").addEventListener("click", fillinWord, false); +function read(s){ + var msg = new SpeechSynthesisUtterance(s); + reader.speak(msg); + console.log("+++++++++++++"); +} + +function onReadClick(){ + isRead = !isRead; + if(!isRead){ + reader.cancel(); + } +} +function onChooseClick(){ + isChoose = !isChoose; +} + -- 2.17.1 From 320a99d4793e03c40cd9f4d8267264ab67bb5ede Mon Sep 17 00:00:00 2001 From: Hui Lan Date: Mon, 24 Apr 2023 11:38:16 +0800 Subject: [PATCH 02/11] Move Tan Yanmei's fillword.js to app/static/js/tanyanmei-fillword.js. Delete the strange folder static js. --- app/static/js/tanyanmei-fillword.js | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/static/js/tanyanmei-fillword.js diff --git a/app/static/js/tanyanmei-fillword.js b/app/static/js/tanyanmei-fillword.js new file mode 100644 index 0000000..49a8bc2 --- /dev/null +++ b/app/static/js/tanyanmei-fillword.js @@ -0,0 +1,33 @@ +isRead = true; +isChoose = true; +var reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停 +reader.cancel();// 初始化时清空reader列表 +function getWord(){ + var word = window.getSelection?window.getSelection():document.selection.createRange().text; + return word; +} +function fillinWord(){ + var word = getWord(); + if (isRead) read(word); + if (!isChoose) return; + var element = document.getElementById("selected-words"); + element.value = element.value + " " + word; +} +document.getElementById("text-content").addEventListener("click", fillinWord, false); +function read(s){ + var msg = new SpeechSynthesisUtterance(s); + reader.speak(msg); + console.log("+++++++++++++"); +} + +function onReadClick(){ + isRead = !isRead; + if(!isRead){ + reader.cancel(); + } +} +function onChooseClick(){ + isChoose = !isChoose; +} + +∑∑ -- 2.17.1 From e9e2bd3d2353b64e05724327210849f1fcf363fc Mon Sep 17 00:00:00 2001 From: Hui Lan Date: Mon, 24 Apr 2023 11:43:43 +0800 Subject: [PATCH 03/11] Remove static\js --- "static\\js/fillword.js" | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 "static\\js/fillword.js" diff --git "a/static\\js/fillword.js" "b/static\\js/fillword.js" deleted file mode 100644 index 9843331..0000000 --- "a/static\\js/fillword.js" +++ /dev/null @@ -1,32 +0,0 @@ -isRead = true; -isChoose = true; -var reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停 -reader.cancel();// 初始化时清空reader列表 -function getWord(){ - var word = window.getSelection?window.getSelection():document.selection.createRange().text; - return word; -} -function fillinWord(){ - var word = getWord(); - if (isRead) read(word); - if (!isChoose) return; - var element = document.getElementById("selected-words"); - element.value = element.value + " " + word; -} -document.getElementById("text-content").addEventListener("click", fillinWord, false); -function read(s){ - var msg = new SpeechSynthesisUtterance(s); - reader.speak(msg); - console.log("+++++++++++++"); -} - -function onReadClick(){ - isRead = !isRead; - if(!isRead){ - reader.cancel(); - } -} -function onChooseClick(){ - isChoose = !isChoose; -} - -- 2.17.1 From ce28b91bd5c3aef42d38f09ffffbd2080d56b503 Mon Sep 17 00:00:00 2001 From: Ni Lingli <1195621865@qq.com> Date: Sat, 6 May 2023 18:18:12 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E5=B1=8F=E5=B9=95=E5=88=B7=E6=96=B0?= =?UTF-8?q?=EF=BC=8C=E5=81=9C=E6=AD=A2=E9=98=85=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/static/js/fillword.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/static/js/fillword.js b/app/static/js/fillword.js index ba249dd..abaac2f 100644 --- a/app/static/js/fillword.js +++ b/app/static/js/fillword.js @@ -65,4 +65,7 @@ function onChooseClick() { function stopRead() { reader.cancel(); -} \ No newline at end of file +} +if (performance.navigation.type == 1) { //如果网页刷新,停止播放声音 + stopRead() + } \ No newline at end of file -- 2.17.1 From df64065dcc64fd14c1e4ab36946d735cb08a0212 Mon Sep 17 00:00:00 2001 From: Ni Lingli <1195621865@qq.com> Date: Sat, 6 May 2023 18:19:24 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E7=82=B9=E5=87=BB=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E7=AF=87=EF=BC=8C=E5=81=9C=E6=AD=A2=E9=98=85=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/templates/userpage_get.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html index b5e16aa..40fa75e 100644 --- a/app/templates/userpage_get.html +++ b/app/templates/userpage_get.html @@ -47,9 +47,9 @@ {# #} {# {% endfor %}#} - 下一篇 Next Article + 下一篇 Next Article {% if session.get('existing_articles') != None and session.get('existing_articles')["index"] !=0 %} - 上一篇 Previous Article + 上一篇 Previous Article {% endif %}

阅读文章并回答问题

-- 2.17.1 From a0ddf4bdadb1b9e045314231c42875cdc9ac2cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=AD=90=E7=9D=BF?= <1342954478@qq.com> Date: Sat, 27 May 2023 17:33:37 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=87=B3=20'app/static/js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复了 Bug492 选中问号出现多个问号的问题。 解决了选中紧跟标点符号的单词,单词能正常显示。 优化了选中较长的文章时页面容易出现卡顿的问题。 --- app/static/js/highlight.js | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/app/static/js/highlight.js b/app/static/js/highlight.js index 0cea31a..927f698 100644 --- a/app/static/js/highlight.js +++ b/app/static/js/highlight.js @@ -22,33 +22,19 @@ function getWord() { function highLight() { if (!isHighlight) return; - let articleContent = document.getElementById("article").innerText; //将原来的.innerText改为.innerHtml,使用innerText会把原文章中所包含的
标签去除,导致处理后的文章内容失去了原来的格式 + let articleContent = document.getElementById("article").innerText; let pickedWords = document.getElementById("selected-words"); // words picked to the text area let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list - let allWords = ""; //初始化allWords的值,避免进入判断后编译器认为allWords未初始化的问题 - if(dictionaryWords != null){//增加一个判断,检查生词本里面是否为空,如果为空,allWords只添加选中的单词 - allWords = pickedWords.value + " " + dictionaryWords.value; - } - else{ - allWords = pickedWords.value + " "; - } - const list = allWords.split(" ");//将所有的生词放入一个list中,用于后续处理 + let allWords = dictionaryWords === null ? pickedWords.value + " " : pickedWords.value + " " + dictionaryWords.value; + const list = allWords.split(" "); // 将所有的生词放入一个list中,用于后续处理 for (let i = 0; i < list.length; ++i) { - list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符 - list[i] = list[i].replace('|', ""); - list[i] = list[i].replace('?', ""); - if (list[i] !== "" && "".indexOf(list[i]) === -1 && "".indexOf(list[i]) === -1) { - //将文章中所有出现该单词word的地方改为:"" + word + ""。 正则表达式RegExp()中,"\\b"代表单词边界匹配。 - - //修改代码 - let articleContent_fb = articleContent; //文章副本 - while(articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()) !== -1 && list[i]!=""){ - //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。 - const index = articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()); - list[i] = articleContent_fb.substring(index, index + list[i].length); - - articleContent_fb = articleContent_fb.substring(index + list[i].length); // 使用副本中list[i]之后的子串替换掉副本 - articleContent = articleContent.replace(new RegExp("\\b"+list[i]+"\\b","g"),"" + list[i] + ""); + list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符 + if (list[i] != "" && "".indexOf(list[i]) === -1 && "".indexOf(list[i]) === -1) { + // 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。 + let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi"))); + for (let word of matches) { + // 将文章中所有出现该单词word的地方改为:"" + word + ""。 + articleContent = articleContent.replace(new RegExp("\\b" + word + "\\b", "g"), "" + word + ""); } } } -- 2.17.1 From cdf6180901b9f1f47005956eef70dea858b3c20e Mon Sep 17 00:00:00 2001 From: Ni Lingli <1195621865@qq.com> Date: Tue, 30 May 2023 18:48:37 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E5=88=B7=E6=96=B0=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=EF=BC=8C=E7=82=B9=E5=87=BB=E4=B8=8A=E4=B8=8B=E7=AF=87=EF=BC=8C?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=E7=94=9F=E8=AF=8D=E5=BA=93=EF=BC=8C=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E9=98=85=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/static/js/fillword.js | 3 +++ app/templates/userpage_get.html | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/static/js/fillword.js b/app/static/js/fillword.js index b967633..57521a7 100644 --- a/app/static/js/fillword.js +++ b/app/static/js/fillword.js @@ -29,3 +29,6 @@ function onReadClick() { function onChooseClick() { isChoose = !isChoose; } +if (performance.navigation.type == 1) { //如果网页刷新,停止播放声音 + stopRead() +} \ No newline at end of file diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html index 68997ef..e503f0b 100644 --- a/app/templates/userpage_get.html +++ b/app/templates/userpage_get.html @@ -49,8 +49,8 @@ {# #} {# {% endfor %}#} - - + +

阅读文章并回答问题

@@ -97,7 +97,7 @@

收集生词吧 (可以在正文中划词,也可以复制黏贴)


- +
{% if session.get['thisWord'] %} -- 2.17.1 From d402bb45cb287da169439410242e1f773e861b3a Mon Sep 17 00:00:00 2001 From: Ni Lingli <1195621865@qq.com> Date: Wed, 31 May 2023 18:37:05 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E5=88=B7=E6=96=B0=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=EF=BC=8C=E7=82=B9=E5=87=BB=E4=B8=8A=E4=B8=8B=E7=AF=87=EF=BC=8C?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=E7=94=9F=E8=AF=8D=E5=BA=93=EF=BC=8C=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E9=98=85=E8=AF=BB(=E6=9B=B4=E6=94=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/static/js/fillword.js | 3 ++- app/templates/userpage_get.html | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/static/js/fillword.js b/app/static/js/fillword.js index 57521a7..317373c 100644 --- a/app/static/js/fillword.js +++ b/app/static/js/fillword.js @@ -29,6 +29,7 @@ function onReadClick() { function onChooseClick() { isChoose = !isChoose; } + if (performance.navigation.type == 1) { //如果网页刷新,停止播放声音 - stopRead() + Reader.stopRead(); } \ No newline at end of file diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html index e503f0b..f7ba8da 100644 --- a/app/templates/userpage_get.html +++ b/app/templates/userpage_get.html @@ -49,8 +49,8 @@ {# #} {# {% endfor %}#} - - + +

阅读文章并回答问题

@@ -97,7 +97,7 @@

收集生词吧 (可以在正文中划词,也可以复制黏贴)


- +
{% if session.get['thisWord'] %} -- 2.17.1 From 682247bff16f94e6572df1e2be5a64120596a3c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=AD=90=E7=9D=BF?= <1342954478@qq.com> Date: Fri, 2 Jun 2023 21:33:21 +0800 Subject: [PATCH 09/11] refactor partial function and code writing specifications --- app/static/js/fillword.js | 3 ++- app/static/js/highlight.js | 48 +++++++++++++-------------------- app/static/js/read.js | 4 +-- app/static/js/word_operation.js | 31 +++++++++------------ 4 files changed, 35 insertions(+), 51 deletions(-) diff --git a/app/static/js/fillword.js b/app/static/js/fillword.js index 317373c..b3a8b42 100644 --- a/app/static/js/fillword.js +++ b/app/static/js/fillword.js @@ -30,6 +30,7 @@ function onChooseClick() { isChoose = !isChoose; } -if (performance.navigation.type == 1) { //如果网页刷新,停止播放声音 +// 如果网页刷新,停止播放声音 +if (performance.getEntriesByType("navigation")[0].type == "reload") { Reader.stopRead(); } \ No newline at end of file diff --git a/app/static/js/highlight.js b/app/static/js/highlight.js index 927f698..76b4793 100644 --- a/app/static/js/highlight.js +++ b/app/static/js/highlight.js @@ -22,48 +22,38 @@ function getWord() { function highLight() { if (!isHighlight) return; - let articleContent = document.getElementById("article").innerText; + let articleContent = document.getElementById("article").innerHTML; // innerHTML保留HTML标签来保持部分格式,且适配不同的浏览器 let pickedWords = document.getElementById("selected-words"); // words picked to the text area let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list let allWords = dictionaryWords === null ? pickedWords.value + " " : pickedWords.value + " " + dictionaryWords.value; - const list = allWords.split(" "); // 将所有的生词放入一个list中,用于后续处理 + const list = allWords.split(" "); // 将所有的生词放入一个list中 + let totalSet = new Set(); for (let i = 0; i < list.length; ++i) { list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符 - if (list[i] != "" && "".indexOf(list[i]) === -1 && "".indexOf(list[i]) === -1) { + if (list[i] != "" && !totalSet.has(list[i])) { // 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。 let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi"))); - for (let word of matches) { - // 将文章中所有出现该单词word的地方改为:"" + word + ""。 - articleContent = articleContent.replace(new RegExp("\\b" + word + "\\b", "g"), "" + word + ""); + if (matches.has("mark")) { + // 优先处理单词为 "mark" 的情况 + totalSet = new Set(["mark", ...totalSet]); } + totalSet = new Set([...totalSet, ...matches]); } + } + // 删除所有的mark标签,防止标签发生嵌套 + articleContent = articleContent.replace(/<(mark)[^>]*>/gi, ""); + articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, ""); + // 将文章中所有出现该单词word的地方改为:"" + word + ""。 + for (let word of totalSet) { + articleContent = articleContent.replace(new RegExp("\\b" + word + "\\b", "g"), "" + word + ""); } document.getElementById("article").innerHTML = articleContent; } function cancelHighlighting() { - let articleContent = document.getElementById("article").innerText;//将原来的.innerText改为.innerHtml,原因同上 - let pickedWords = document.getElementById("selected-words"); - const dictionaryWords = document.getElementById("selected-words2"); - const list = pickedWords.value.split(" "); - if (pickedWords != null) { - for (let i = 0; i < list.length; ++i) { - list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); - if (list[i] !== "") { //原来判断的代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容 - articleContent = articleContent.replace(new RegExp(""+list[i]+"", "g"), list[i]); - } - } - } - if (dictionaryWords != null) { - let list2 = pickedWords.value.split(" "); - for (let i = 0; i < list2.length; ++i) { - list2 = dictionaryWords.value.split(" "); - list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, ""); - if (list2[i] !== "") { //原来代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容 - articleContent = articleContent.replace(new RegExp(""+list2[i]+"", "g"), list2[i]); - } - } - } + let articleContent = document.getElementById("article").innerHTML; + articleContent = articleContent.replace(/<(mark)[^>]*>/gi, ""); + articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, ""); document.getElementById("article").innerHTML = articleContent; } @@ -85,4 +75,4 @@ function toggleHighlighting() { } } -showBtnHandler(); +showBtnHandler(); \ No newline at end of file diff --git a/app/static/js/read.js b/app/static/js/read.js index 814f627..c28fd26 100644 --- a/app/static/js/read.js +++ b/app/static/js/read.js @@ -9,7 +9,7 @@ var Reader = (function() { msg.rate = rate; msg.lang = "en-US"; msg.onboundary = ev => { - if (ev.name == "word") { + if (ev.name === "word") { current_position = ev.charIndex; } } @@ -32,4 +32,4 @@ var Reader = (function() { read: read, stopRead: stopRead }; -})(); +}) (); diff --git a/app/static/js/word_operation.js b/app/static/js/word_operation.js index f043cce..dcf38ff 100644 --- a/app/static/js/word_operation.js +++ b/app/static/js/word_operation.js @@ -5,15 +5,14 @@ function familiar(theWord) { $.ajax({ type:"GET", url:"/" + username + "/" + word + "/familiar", - success:function(response){ + success:function(response) { let new_freq = freq - 1; const allow_move = document.getElementById("move_dynamiclly").checked; if (allow_move) { - if (new_freq <= 0) { removeWord(theWord); } else { - renderWord({ word: theWord, freq: new_freq }); + renderWord({word: theWord, freq: new_freq}); } } else { if(new_freq <1) { @@ -33,11 +32,11 @@ function unfamiliar(theWord) { $.ajax({ type:"GET", url:"/" + username + "/" + word + "/unfamiliar", - success:function(response){ + success:function(response) { let new_freq = parseInt(freq) + 1; const allow_move = document.getElementById("move_dynamiclly").checked; if (allow_move) { - renderWord({ word: theWord, freq: new_freq }); + renderWord({word: theWord, freq: new_freq}); } else { $("#freq_" + theWord).text(new_freq); } @@ -51,7 +50,7 @@ function delete_word(theWord) { $.ajax({ type:"GET", url:"/" + username + "/" + word + "/del", - success:function(response){ + success:function(response) { const allow_move = document.getElementById("move_dynamiclly").checked; if (allow_move) { removeWord(theWord); @@ -114,7 +113,7 @@ function removeWord(word) { // 根据词频信息删除元素 word = word.replace('&', '&'); const element_to_remove = document.getElementById(`p_${word}`); - if (element_to_remove != null) { + if (element_to_remove !== null) { element_to_remove.remove(); } } @@ -129,7 +128,7 @@ function renderWord(word) { for (const current of container.children) { const cur_word = parseWord(current); // 找到第一个词频比它小的元素,插入到这个元素前面 - if (compareWord(cur_word, word) == -1) { + if (compareWord(cur_word, word) === -1) { container.insertBefore(new_element, current); inserted = true; break; @@ -165,17 +164,11 @@ function elementFromString(string) { * 当first大于second时返回1 */ function compareWord(first, second) { - if (first.freq < second.freq) { - return -1; + if (first.freq !== second.freq) { + return first.freq < second.freq ? -1 : 1; } - if (first.freq > second.freq) { - return 1; - } - if (first.word < second.word) { - return -1; - } - if (first.word > second.word) { - return 1; + if (first.word !== second.word) { + return first.word < second.word ? -1 : 1; } return 0; -} +} \ No newline at end of file -- 2.17.1 From 9816596cf8942406bf7ae3e66dfdda3c31464da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=AD=90=E7=9D=BF?= <1342954478@qq.com> Date: Fri, 2 Jun 2023 21:34:27 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E5=88=A0=E9=99=A4=20'app/static/js/tanya?= =?UTF-8?q?nmei-fillword.js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/static/js/tanyanmei-fillword.js | 33 ----------------------------- 1 file changed, 33 deletions(-) delete mode 100644 app/static/js/tanyanmei-fillword.js diff --git a/app/static/js/tanyanmei-fillword.js b/app/static/js/tanyanmei-fillword.js deleted file mode 100644 index 49a8bc2..0000000 --- a/app/static/js/tanyanmei-fillword.js +++ /dev/null @@ -1,33 +0,0 @@ -isRead = true; -isChoose = true; -var reader = window.speechSynthesis; // 全局定义朗读者,以便朗读和暂停 -reader.cancel();// 初始化时清空reader列表 -function getWord(){ - var word = window.getSelection?window.getSelection():document.selection.createRange().text; - return word; -} -function fillinWord(){ - var word = getWord(); - if (isRead) read(word); - if (!isChoose) return; - var element = document.getElementById("selected-words"); - element.value = element.value + " " + word; -} -document.getElementById("text-content").addEventListener("click", fillinWord, false); -function read(s){ - var msg = new SpeechSynthesisUtterance(s); - reader.speak(msg); - console.log("+++++++++++++"); -} - -function onReadClick(){ - isRead = !isRead; - if(!isRead){ - reader.cancel(); - } -} -function onChooseClick(){ - isChoose = !isChoose; -} - -∑∑ -- 2.17.1 From 616d35a92f38e65a68be0168dfd04b4ea0853c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=AD=90=E7=9D=BF?= <1342954478@qq.com> Date: Mon, 5 Jun 2023 08:37:31 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=9A=84=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/static/js/fillword.js | 1 + app/static/js/highlight.js | 11 ++++++----- app/static/js/word_operation.js | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/static/js/fillword.js b/app/static/js/fillword.js index b3a8b42..98c4ba5 100644 --- a/app/static/js/fillword.js +++ b/app/static/js/fillword.js @@ -31,6 +31,7 @@ function onChooseClick() { } // 如果网页刷新,停止播放声音 +// performance.navigation.type 以弃用,改为了performance.getEntriesByType("navigation")[0].type if (performance.getEntriesByType("navigation")[0].type == "reload") { Reader.stopRead(); } \ No newline at end of file diff --git a/app/static/js/highlight.js b/app/static/js/highlight.js index 76b4793..87e2b87 100644 --- a/app/static/js/highlight.js +++ b/app/static/js/highlight.js @@ -22,19 +22,19 @@ function getWord() { function highLight() { if (!isHighlight) return; - let articleContent = document.getElementById("article").innerHTML; // innerHTML保留HTML标签来保持部分格式,且适配不同的浏览器 + let articleContent = document.getElementById("article").innerHTML; // 相比innerText,innerHTML保留HTML标签,来保持文章原先的格式,且适配不同的浏览器。 let pickedWords = document.getElementById("selected-words"); // words picked to the text area let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list let allWords = dictionaryWords === null ? pickedWords.value + " " : pickedWords.value + " " + dictionaryWords.value; - const list = allWords.split(" "); // 将所有的生词放入一个list中 - let totalSet = new Set(); + const list = allWords.split(" "); // 将所有的生词放入一个list中。 + let totalSet = new Set(); // 通过集合存储所有单词,通过判断重复单词,以及最后只遍历一次的方法,提高运行效率。 for (let i = 0; i < list.length; ++i) { - list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符 + list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符。 if (list[i] != "" && !totalSet.has(list[i])) { // 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。 let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi"))); if (matches.has("mark")) { - // 优先处理单词为 "mark" 的情况 + // 优先处理单词为 "mark" 的情况,防止和标签产生冲突。 totalSet = new Set(["mark", ...totalSet]); } totalSet = new Set([...totalSet, ...matches]); @@ -51,6 +51,7 @@ function highLight() { } function cancelHighlighting() { + // 通过innerHTML获取文章后,去掉标签即可实现取消高亮的功能,不需要遍历单词逐个修改。 let articleContent = document.getElementById("article").innerHTML; articleContent = articleContent.replace(/<(mark)[^>]*>/gi, ""); articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, ""); diff --git a/app/static/js/word_operation.js b/app/static/js/word_operation.js index dcf38ff..933866c 100644 --- a/app/static/js/word_operation.js +++ b/app/static/js/word_operation.js @@ -164,6 +164,7 @@ function elementFromString(string) { * 当first大于second时返回1 */ function compareWord(first, second) { + // 优化了代码行数 if (first.freq !== second.freq) { return first.freq < second.freq ? -1 : 1; } -- 2.17.1