Merge branch 'Alpha-snapshot20230621' of http://118.25.96.118:3000/mrlan/EnglishPal into Bug553_LinShan

Bug553_LinShan
林杉 2024-04-24 18:15:04 +08:00
commit b626df7c4b
7 changed files with 174 additions and 54 deletions

View File

@ -1,5 +1,6 @@
let isRead = true; // initialize from localStorage
let isChoose = true; let isRead = localStorage.getItem('readChecked') !== 'false'; // default to true
let isChoose = localStorage.getItem('chooseChecked') !== 'false';
function getWord() { function getWord() {
return window.getSelection ? window.getSelection() : document.selection.createRange().text; return window.getSelection ? window.getSelection() : document.selection.createRange().text;
@ -11,6 +12,7 @@ function fillInWord() {
if (!isChoose) return; if (!isChoose) return;
const element = document.getElementById("selected-words"); const element = document.getElementById("selected-words");
element.value = element.value + " " + word; element.value = element.value + " " + word;
localStorage.setItem('selectedWords', element.value);
} }
document.getElementById("text-content").addEventListener("click", fillInWord, false); document.getElementById("text-content").addEventListener("click", fillInWord, false);
@ -24,10 +26,12 @@ inputSlider.oninput = () => {
function onReadClick() { function onReadClick() {
isRead = !isRead; isRead = !isRead;
localStorage.setItem('readChecked', isRead);
} }
function onChooseClick() { function onChooseClick() {
isChoose = !isChoose; isChoose = !isChoose;
localStorage.setItem('chooseChecked', isChoose);
} }
// 如果网页刷新,停止播放声音 // 如果网页刷新,停止播放声音

View File

@ -1,4 +1,4 @@
let isHighlight = true; let isHighlight = localStorage.getItem('highlightChecked') !== 'false'; // default to true
function cancelBtnHandler() { function cancelBtnHandler() {
cancelHighlighting(); cancelHighlighting();
@ -73,6 +73,7 @@ function toggleHighlighting() {
isHighlight = true; isHighlight = true;
highLight(); highLight();
} }
localStorage.setItem('highlightChecked', isHighlight);
} }
showBtnHandler(); showBtnHandler();

View File

@ -34,9 +34,9 @@
<div class="alert alert-success" role="alert">共有文章 <span class="badge bg-success"> {{ number_of_essays }} </span></div> <div class="alert alert-success" role="alert">共有文章 <span class="badge bg-success"> {{ number_of_essays }} </span></div>
<p>粘贴1篇文章 (English only)</p> <p>粘贴1篇文章 (English only)</p>
<form method="post" action="/"> <form method="post" action="/">
<textarea name="content" rows="10" cols="120"></textarea><br/> <textarea name="content" id="article" rows="10" cols="120"></textarea><br/>
<input type="submit" value="get文章中的词频"/> <input type="submit" value="get文章中的词频"/>
<input type="reset" value="清除"/> <input type="reset" value="清除" onclick="clearArticle()"/>
</form> </form>
{% if d_len > 0 %} {% if d_len > 0 %}
<p><b>最常见的词</b></p> <p><b>最常见的词</b></p>
@ -53,5 +53,22 @@
<script src="{{ js }}" ></script> <script src="{{ js }}" ></script>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<script type="text/javascript">
// IIFE, avoid polluting the global scope
(function() {
const articleInput = document.querySelector('#article');
articleInput.value = localStorage.getItem('article') || '';
articleInput.addEventListener('input', function() {
localStorage.setItem('article', articleInput.value);
});
window.clearArticle = function() {
localStorage.removeItem('article');
articleInput.value = '';
};
})();
</script>
</body> </body>
</html> </html>

View File

@ -109,22 +109,22 @@
</div> </div>
</div> </div>
<input type="checkbox" onclick="toggleHighlighting()" checked/>生词高亮 <input type="checkbox" id="highlightCheckbox" onclick="toggleHighlighting()" />生词高亮
<input type="checkbox" onclick="onReadClick()" checked/>大声朗读 <input type="checkbox" id="readCheckbox" onclick="onReadClick()" />大声朗读
<input type="checkbox" onclick="onChooseClick()" checked/>划词入库 <input type="checkbox" id="chooseCheckbox" onclick="onChooseClick()" />划词入库
<div class="range"> <div class="range">
<div class="field"> <div class="field">
<div class="sliderValue"> <div class="sliderValue">
<span id="rangeValue">1×</span> <span id="rangeValue">1×</span>
</div> </div>
<input type="range" id="rangeComponent" min="0.5" max="2" value="1" step="0.25"/> <input type="range" id="rangeComponent" min="0.5" max="2" value="1" step="0.25" />
</div> </div>
</div> </div>
<p><b>收集生词吧</b> (可以在正文中划词,也可以复制黏贴)</p> <p><b>收集生词吧</b> (可以在正文中划词,也可以复制黏贴)</p>
<form method="post" action="/{{ username }}/userpage"> <form method="post" action="/{{ username }}/userpage">
<textarea name="content" id="selected-words" rows="10" cols="120"></textarea><br/> <textarea name="content" id="selected-words" rows="10" cols="120"></textarea><br/>
<button class="btn btn-primary btn-lg" type="submit" onclick="Reader.stopRead()">把生词加入我的生词库</button> <button class="btn btn-primary btn-lg" type="submit" onclick="Reader.stopRead()">把生词加入我的生词库</button>
<button class="btn btn-primary btn-lg" type="reset">清除</button> <button class="btn btn-primary btn-lg" type="reset" onclick="clearSelectedWords()">清除</button>
</form> </form>
{% if session.get['thisWord'] %} {% if session.get['thisWord'] %}
<script type="text/javascript"> <script type="text/javascript">
@ -174,11 +174,54 @@
{% endif %} {% endif %}
<script type="text/javascript"> <script type="text/javascript">
window.onload = function () { // 页面加载时执行 window.onload = function () { // 页面加载时执行
const settings = {
// initialize settings from localStorage
highlightChecked: localStorage.getItem('highlightChecked') !== 'false', // localStorage stores strings, default to true. same below
readChecked: localStorage.getItem('readChecked') !== 'false',
chooseChecked: localStorage.getItem('chooseChecked') !== 'false',
rangeValue: localStorage.getItem('rangeValue') || '1',
selectedWords: localStorage.getItem('selectedWords') || ''
};
const elements = {
highlightCheckbox: document.querySelector('#highlightCheckbox'),
readCheckbox: document.querySelector('#readCheckbox'),
chooseCheckbox: document.querySelector('#chooseCheckbox'),
rangeComponent: document.querySelector('#rangeComponent'),
rangeValueDisplay: document.querySelector('#rangeValue'),
selectedWordsInput: document.querySelector('#selected-words')
};
// 应用设置到页面元素
elements.highlightCheckbox.checked = settings.highlightChecked;
elements.readCheckbox.checked = settings.readChecked;
elements.chooseCheckbox.checked = settings.chooseChecked;
elements.rangeComponent.value = settings.rangeValue;
elements.rangeValueDisplay.textContent = `${settings.rangeValue}x`;
elements.selectedWordsInput.value = settings.selectedWords;
// 刷新页面或进入页面时判断,若不是首篇文章,则上一篇按钮可见 // 刷新页面或进入页面时判断,若不是首篇文章,则上一篇按钮可见
if(sessionStorage.getItem('pre_page_button')!="display" && sessionStorage.getItem('pre_page_button')){ if (sessionStorage.getItem('pre_page_button') !== 'display' && sessionStorage.getItem('pre_page_button')) {
$('#load_pre_article').show(); $('#load_pre_article').show();
} }
// 事件监听器
elements.selectedWordsInput.addEventListener('input', () => {
localStorage.setItem('selectedWords', elements.selectedWordsInput.value);
});
elements.rangeComponent.addEventListener('input', () => {
const rangeValue = elements.rangeComponent.value;
elements.rangeValueDisplay.textContent = `${rangeValue}x`;
localStorage.setItem('rangeValue', rangeValue);
});
}; };
function clearSelectedWords() {
localStorage.removeItem('selectedWords');
document.querySelector('#selected-words').value = '';
}
function load_next_article(){ function load_next_article(){
$.ajax({ $.ajax({
url: '/get_next_article/{{username}}', url: '/get_next_article/{{username}}',

View File

@ -26,7 +26,7 @@
<input type="button" id="btn-selection" value="全部勾选" onclick="toggleCheckboxSelection(true)" /> <input type="button" id="btn-selection" value="全部勾选" onclick="toggleCheckboxSelection(true)" />
</p> </p>
<form method="post" action="/{{username}}/mark"> <form method="post" action="/{{username}}/mark">
<button type="submit" name="add-btn" class="btn btn-outline-primary btn-lg">加入我的生词簿</button> <button type="submit" name="add-btn" class="btn btn-outline-primary btn-lg" onclick="clearSelectedWords()">加入我的生词簿</button>
{% for x in lst %} {% for x in lst %}
{% set word = x[0]%} {% set word = x[0]%}
<p> <p>

View File

@ -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()

View File

@ -4,3 +4,5 @@ PyYAML~=6.0
pony==0.7.16 pony==0.7.16
snowballstemmer==2.2.0 snowballstemmer==2.2.0
Werkzeug==2.2.2 Werkzeug==2.2.2
pytest~=8.1.1