forked from mrlan/EnglishPal
				
			Merge pull request 'Bug544-TangXinyuan' (#105) from Bug544-TangXinyuan into Alpha-snapshot20230621
Reviewed-on: mrlan/EnglishPal#105Bug476-LiMengdie
						commit
						2d9aa8fa61
					
				|  | @ -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,13 +26,15 @@ 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); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // 如果网页刷新,停止播放声音
 | // 如果网页刷新,停止播放声音
 | ||||||
| if (performance.getEntriesByType("navigation")[0].type == "reload") { | if (performance.getEntriesByType("navigation")[0].type == "reload") { | ||||||
|     Reader.stopRead(); |     Reader.stopRead(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| let isHighlight = true; | let isHighlight = localStorage.getItem('highlightChecked') !== 'false'; // default to true
 | ||||||
| 
 | 
 | ||||||
| function cancelBtnHandler() { | function cancelBtnHandler() { | ||||||
|     cancelHighlighting(); |     cancelHighlighting(); | ||||||
|  | @ -39,7 +39,7 @@ function highLight() { | ||||||
|             } |             } | ||||||
|             totalSet = new Set([...totalSet, ...matches]); |             totalSet = new Set([...totalSet, ...matches]); | ||||||
|         } |         } | ||||||
|     }  |     } | ||||||
|     // 删除所有的mark标签,防止标签发生嵌套
 |     // 删除所有的mark标签,防止标签发生嵌套
 | ||||||
|     articleContent = articleContent.replace(/<(mark)[^>]*>/gi, ""); |     articleContent = articleContent.replace(/<(mark)[^>]*>/gi, ""); | ||||||
|     articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, ""); |     articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, ""); | ||||||
|  | @ -73,6 +73,7 @@ function toggleHighlighting() { | ||||||
|         isHighlight = true; |         isHighlight = true; | ||||||
|         highLight(); |         highLight(); | ||||||
|     } |     } | ||||||
|  |      localStorage.setItem('highlightChecked', isHighlight); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| showBtnHandler(); | showBtnHandler(); | ||||||
|  | @ -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> | ||||||
|  |  | ||||||
|  | @ -86,7 +86,7 @@ | ||||||
|             <div> |             <div> | ||||||
|                 <p><small class="text-muted" id="source">{{ today_article['source'] }}</small></p><br/> |                 <p><small class="text-muted" id="source">{{ today_article['source'] }}</small></p><br/> | ||||||
|             </div> |             </div> | ||||||
|              | 
 | ||||||
|             <p><b id="question">{{ today_article['question'] }}</b></p><br/> |             <p><b id="question">{{ today_article['question'] }}</b></p><br/> | ||||||
|                 <script type="text/javascript"> |                 <script type="text/javascript"> | ||||||
|                     function toggle_visibility(id) { {# https://css-tricks.com/snippets/javascript/showhide-element/#} |                     function toggle_visibility(id) { {# https://css-tricks.com/snippets/javascript/showhide-element/#} | ||||||
|  | @ -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"> | ||||||
|  | @ -139,7 +139,7 @@ | ||||||
| 
 | 
 | ||||||
|     {% if d_len > 0 %} |     {% if d_len > 0 %} | ||||||
|         <p> |         <p> | ||||||
|             <b>我的生词簿</b>  |             <b>我的生词簿</b> | ||||||
|             <label for="move_dynamiclly"> |             <label for="move_dynamiclly"> | ||||||
|                 <input type="checkbox" name="move_dynamiclly" id="move_dynamiclly" checked> |                 <input type="checkbox" name="move_dynamiclly" id="move_dynamiclly" checked> | ||||||
|                 允许动态调整顺序 |                 允许动态调整顺序 | ||||||
|  | @ -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}}', | ||||||
|  |  | ||||||
|  | @ -1,50 +1,50 @@ | ||||||
| <!DOCTYPE html> | <!DOCTYPE html> | ||||||
| <html lang="en"> | <html lang="en"> | ||||||
|     <head> |     <head> | ||||||
| 	<meta charset="UTF-8"> |        <meta charset="UTF-8"> | ||||||
| 	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=3.0, user-scalable=yes" /> |        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=3.0, user-scalable=yes" /> | ||||||
| 	<meta name="format-detection" content="telephone=no" /> |        <meta name="format-detection" content="telephone=no" /> | ||||||
| 
 | 
 | ||||||
| 	{{ yml['header'] | safe }} |        {{ yml['header'] | safe }} | ||||||
| 	{% if yml['css']['item'] %} |        {% if yml['css']['item'] %} | ||||||
|         {% for css in yml['css']['item'] %} |          {% for css in yml['css']['item'] %} | ||||||
|         <link href="{{ css }}" rel="stylesheet"> |          <link href="{{ css }}" rel="stylesheet"> | ||||||
|         {% endfor %} |          {% endfor %} | ||||||
| 	{% endif %} |         {% endif %} | ||||||
| 	{% if yml['js']['head'] %} |         {% if yml['js']['head'] %} | ||||||
|         {% for js in yml['js']['head'] %} |          {% for js in yml['js']['head'] %} | ||||||
|         <script src="{{ js }}" ></script> |          <script src="{{ js }}" ></script> | ||||||
|         {% endfor %} |          {% endfor %} | ||||||
| 	{% endif %} |         {% endif %} | ||||||
| 
 | 
 | ||||||
| 	<title>EnglishPal Study Room for {{username}}</title> |        <title>EnglishPal Study Room for {{username}}</title> | ||||||
|     </head> |     </head> | ||||||
|     <body> |     <body> | ||||||
| 	<div class="container-fluid"> |       <div class="container-fluid"> | ||||||
| 	    <p class="mt-md-3"> |           <p class="mt-md-3"> | ||||||
| 		<input type="button" id="btn-cancel-selection" value="取消勾选" onclick="toggleCheckboxSelection(false)" /> |                <input type="button" id="btn-cancel-selection" value="取消勾选" onclick="toggleCheckboxSelection(false)" /> | ||||||
| 		<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> | ||||||
| 		    <font color="grey">{{loop.index}}</font> |                     <font color="grey">{{loop.index}}</font> | ||||||
| 		    : |                     : | ||||||
| 		    <a href='http://youdao.com/w/eng/{{word}}/#keyfrom=dict2.index' title={{word}}>{{word}}</a> |                     <a href='http://youdao.com/w/eng/{{word}}/#keyfrom=dict2.index' title={{word}}>{{word}}</a> | ||||||
| 		    ({{x[1]}}) |                     ({{x[1]}}) | ||||||
| 		    <input type="checkbox" name="marked" value="{{word}}" checked> |                     <input type="checkbox" name="marked" value="{{word}}" checked> | ||||||
| 		</p> |               </p> | ||||||
| 
 | 
 | ||||||
| 		{% endfor %} |                {% endfor %} | ||||||
| 	    </form> |            </form> | ||||||
| 	    {{ yml['footer'] | safe }} |            {{ yml['footer'] | safe }} | ||||||
| 	    {% if yml['js']['bottom'] %} |            {% if yml['js']['bottom'] %} | ||||||
|             {% for js in yml['js']['bottom'] %} |             {% for js in yml['js']['bottom'] %} | ||||||
|             <script src="{{ js }}" ></script> |              <script src="{{ js }}" ></script> | ||||||
|             {% endfor %} |             {% endfor %} | ||||||
| 	    {% endif %} |         {% endif %} | ||||||
| 	</div> |       </div> | ||||||
|     </body> |     </body> | ||||||
| </html> | </html> | ||||||
|  |  | ||||||
|  | @ -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() | ||||||
|  | @ -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 | ||||||
		Loading…
	
		Reference in New Issue