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] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=9A=84=E6=B3=A8=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; }