diff --git a/app/static/js/fillword.js b/app/static/js/fillword.js index b3a8b42..18461f0 100644 --- a/app/static/js/fillword.js +++ b/app/static/js/fillword.js @@ -1,5 +1,6 @@ -let isRead = true; -let isChoose = true; +// initialize from localStorage +let isRead = localStorage.getItem('readChecked') !== 'false'; // default to true +let isChoose = localStorage.getItem('chooseChecked') !== 'false'; function getWord() { return window.getSelection ? window.getSelection() : document.selection.createRange().text; @@ -11,6 +12,7 @@ function fillInWord() { if (!isChoose) return; const element = document.getElementById("selected-words"); element.value = element.value + " " + word; + localStorage.setItem('selectedWords', element.value); } document.getElementById("text-content").addEventListener("click", fillInWord, false); @@ -24,13 +26,15 @@ inputSlider.oninput = () => { function onReadClick() { isRead = !isRead; + localStorage.setItem('readChecked', isRead); } function onChooseClick() { isChoose = !isChoose; + localStorage.setItem('chooseChecked', isChoose); } // 如果网页刷新,停止播放声音 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..e8a3cf8 100644 --- a/app/static/js/highlight.js +++ b/app/static/js/highlight.js @@ -1,4 +1,4 @@ -let isHighlight = true; +let isHighlight = localStorage.getItem('highlightChecked') !== 'false'; // default to true function cancelBtnHandler() { cancelHighlighting(); @@ -39,7 +39,7 @@ function highLight() { } totalSet = new Set([...totalSet, ...matches]); } - } + } // 删除所有的mark标签,防止标签发生嵌套 articleContent = articleContent.replace(/<(mark)[^>]*>/gi, ""); articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, ""); @@ -73,6 +73,7 @@ function toggleHighlighting() { isHighlight = true; highLight(); } + localStorage.setItem('highlightChecked', isHighlight); } showBtnHandler(); \ No newline at end of file diff --git a/app/templates/mainpage_get.html b/app/templates/mainpage_get.html index e96e8dc..3ba0ece 100644 --- a/app/templates/mainpage_get.html +++ b/app/templates/mainpage_get.html @@ -34,9 +34,9 @@

粘贴1篇文章 (English only)

-
+
- +
{% if d_len > 0 %}

最常见的词

@@ -53,5 +53,22 @@ {% endfor %} {% endif %} + diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html index 8c684b6..edb8bf2 100644 --- a/app/templates/userpage_get.html +++ b/app/templates/userpage_get.html @@ -86,7 +86,7 @@

{{ today_article['source'] }}


- +

{{ today_article['question'] }}


- {% endfor %} - {% endif %} + {{ yml['header'] | safe }} + {% if yml['css']['item'] %} + {% for css in yml['css']['item'] %} + + {% endfor %} + {% endif %} + {% if yml['js']['head'] %} + {% for js in yml['js']['head'] %} + + {% endfor %} + {% endif %} - EnglishPal Study Room for {{username}} + EnglishPal Study Room for {{username}} -
-

- - -

-
- - {% for x in lst %} - {% set word = x[0]%} -

- {{loop.index}} - : - {{word}} - ({{x[1]}}) - -

+
+

+ + +

+ + + {% for x in lst %} + {% set word = x[0]%} +

+ {{loop.index}} + : + {{word}} + ({{x[1]}}) + +

- {% endfor %} - - {{ yml['footer'] | safe }} - {% if yml['js']['bottom'] %} + {% endfor %} + + {{ yml['footer'] | safe }} + {% if yml['js']['bottom'] %} {% for js in yml['js']['bottom'] %} - + {% endfor %} - {% endif %} -
+ {% endif %} +
diff --git a/app/test/test_bug544_tangxinyuan.py b/app/test/test_bug544_tangxinyuan.py new file mode 100644 index 0000000..43bfa8c --- /dev/null +++ b/app/test/test_bug544_tangxinyuan.py @@ -0,0 +1,53 @@ +import random +import string +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +from helper import signup + + +def has_punctuation(s): + return any(c in string.punctuation for c in s) + + +def login(driver, home, uname, password): + driver.get(home) + WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, '登录'))).click() + driver.find_element(By.ID, 'username').send_keys(uname) + driver.find_element(By.ID, 'password').send_keys(password) + driver.find_element(By.XPATH, '//button[text()="登录"]').click() + WebDriverWait(driver, 10).until(EC.title_is(f"EnglishPal Study Room for {uname}")) + + +def select_valid_word(driver): + elem = driver.find_element(By.ID, 'text-content') + essay_content = elem.text + valid_word = random.choice([word for word in essay_content.split() if len(word) >= 6 and not has_punctuation( + word) and 'font>' not in word and 'br>' not in word and 'p>' not in word]) + driver.find_element(By.ID, 'selected-words').send_keys(valid_word) + return valid_word + + +def test_save_selected_word(driver, URL): + try: + username, password = signup(URL, driver) + word = select_valid_word(driver) + stored_words = driver.execute_script('return localStorage.getItem("selectedWords");') + assert word == stored_words, "Selected word not saved to localStorage correctly" + # 退出并重新登录以检查存储的单词 + driver.find_element(By.LINK_TEXT, '退出').click() + driver.execute_script("window.open('');window.close();") + + # 等待一会儿,让浏览器有足够的时间关闭标签页 + WebDriverWait(driver, 2) + + # 重新打开一个新的标签页 + driver.execute_script("window.open('');") + driver.switch_to.window(driver.window_handles[-1]) # 切换到新打开的标签页 + + login(driver, URL, username, password) + textarea_content = driver.find_element(By.ID, 'selected-words').get_attribute('value') + assert word == textarea_content, "Selected word not preserved after re-login" + finally: + driver.quit() diff --git a/requirements.txt b/requirements.txt index 4322f80..4b81c20 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ PyYAML~=6.0 pony==0.7.16 snowballstemmer==2.2.0 Werkzeug==2.2.2 + +pytest~=8.1.1 \ No newline at end of file