refactor: Add comments & optimize code

Bug-561
AliasJeff 2024-04-09 21:21:19 +08:00
parent 2966a8162f
commit 83491ce28c
4 changed files with 52 additions and 36 deletions

View File

@ -1,4 +1,5 @@
let isRead = localStorage.getItem('readChecked') !== 'false'; // initialize from localStorage
let isRead = localStorage.getItem('readChecked') !== 'false'; // default to true
let isChoose = localStorage.getItem('chooseChecked') !== 'false'; let isChoose = localStorage.getItem('chooseChecked') !== 'false';
function getWord() { function getWord() {

View File

@ -1,4 +1,4 @@
let isHighlight = localStorage.getItem('highlightChecked') !== 'false'; let isHighlight = localStorage.getItem('highlightChecked') !== 'false'; // default to true
function cancelBtnHandler() { function cancelBtnHandler() {
cancelHighlighting(); cancelHighlighting();

View File

@ -54,18 +54,20 @@
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<script type="text/javascript"> <script type="text/javascript">
window.onload = function() { // IIFE, avoid polluting the global scope
document.getElementById('article').value = localStorage.getItem('article') || ''; (function() {
} const articleInput = document.querySelector('#article');
document.getElementById('article').addEventListener('input', saveArticle); articleInput.value = localStorage.getItem('article') || '';
function saveArticle() { articleInput.addEventListener('input', function() {
localStorage.setItem('article', document.getElementById('article').value); localStorage.setItem('article', articleInput.value);
} });
function clearArticle() { window.clearArticle = function() {
localStorage.removeItem('article'); localStorage.removeItem('article');
} articleInput.value = '';
};
})();
</script> </script>
</body> </body>

View File

@ -117,7 +117,7 @@
<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" onchange="saveRangeValue()" /> <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>
@ -174,40 +174,53 @@
{% endif %} {% endif %}
<script type="text/javascript"> <script type="text/javascript">
window.onload = function () { // 页面加载时执行 window.onload = function () { // 页面加载时执行
const highlightChecked = localStorage.getItem('highlightChecked') !== 'false'; const settings = {
const readChecked = localStorage.getItem('readChecked') !== 'false'; // initialize settings from localStorage
const chooseChecked = localStorage.getItem('chooseChecked') !== 'false'; highlightChecked: localStorage.getItem('highlightChecked') !== 'false', // localStorage stores strings, default to true. same below
const rangeValue = localStorage.getItem('rangeValue') || '1'; readChecked: localStorage.getItem('readChecked') !== 'false',
const selectedWords = localStorage.getItem('selectedWords') || ''; chooseChecked: localStorage.getItem('chooseChecked') !== 'false',
rangeValue: localStorage.getItem('rangeValue') || '1',
selectedWords: localStorage.getItem('selectedWords') || ''
};
document.getElementById('highlightCheckbox').checked = highlightChecked; const elements = {
document.getElementById('readCheckbox').checked = readChecked; highlightCheckbox: document.querySelector('#highlightCheckbox'),
document.getElementById('chooseCheckbox').checked = chooseChecked; readCheckbox: document.querySelector('#readCheckbox'),
document.getElementById('rangeComponent').value = rangeValue; chooseCheckbox: document.querySelector('#chooseCheckbox'),
document.getElementById('rangeValue').innerHTML = rangeValue + 'x'; rangeComponent: document.querySelector('#rangeComponent'),
document.getElementById('selected-words').value = selectedWords; 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();
} }
};
document.getElementById('selected-words').addEventListener('input', saveSelectedWords);
function saveSelectedWords() { // 事件监听器
let selectedWordsTextarea = document.getElementById('selected-words'); elements.selectedWordsInput.addEventListener('input', () => {
localStorage.setItem('selectedWords', selectedWordsTextarea.value); 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() { function clearSelectedWords() {
localStorage.removeItem('selectedWords'); localStorage.removeItem('selectedWords');
document.querySelector('#selected-words').value = '';
} }
function saveRangeValue() {
const rangeValue = document.getElementById('rangeComponent').value;
document.getElementById('rangeValue').textContent = rangeValue;
localStorage.setItem('rangeValue', rangeValue);
}
function load_next_article(){ function load_next_article(){
$.ajax({ $.ajax({