From d83809dcc7e6d9eb6fe950874ac8fa75bf742fce Mon Sep 17 00:00:00 2001 From: xiongjiaming <3190186054@qq.com> Date: Mon, 3 Jun 2024 13:49:53 +0800 Subject: [PATCH 1/3] Fix bug 540 --- app/templates/userpage_get.html | 84 ++++++++++++++++++++++++++++ app/test/test_bug540_xiongjiaming.py | 45 +++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 app/test/test_bug540_xiongjiaming.py diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html index 571051a..762221f 100644 --- a/app/templates/userpage_get.html +++ b/app/templates/userpage_get.html @@ -114,6 +114,11 @@ 生词高亮 大声朗读 划词入库 + +
@@ -297,6 +302,85 @@ $('#read_all').show(); } } + function saveArticle() { + const article = { + user_level: document.getElementById('user_level').innerText, + text_level: document.getElementById('text_level').innerText, + date: document.getElementById('date').innerText.replace('Article added on: ', ''), + article_title: document.getElementById('article_title').innerText, + article_body: document.getElementById('article').innerText, + source: document.getElementById('source').innerText, + question: document.getElementById('question').innerText, + answer: document.getElementById('answer').innerText + }; + + const articleJSON = JSON.stringify(article); + const articleTitle = article.article_title; + const savedArticlesDropdown = document.getElementById('saved_articles_dropdown'); + + var option = document.createElement('option'); + option.text = articleTitle; + option.value = articleJSON; // 存储序列化的JSON字符串 + option.title = article.article_title; + savedArticlesDropdown.appendChild(option); + localStorage.setItem(articleTitle, articleJSON); // 以文章标题为键,序列化的JSON字符串为值存储 + alert("文章已标记"); +} + function loadSelectedArticle() { + const selectedOption = document.getElementById('saved_articles_dropdown'); + const selectedTitle = selectedOption.options[selectedOption.selectedIndex].text; + const articleJSON = localStorage.getItem(selectedTitle); + + if (articleJSON) { + const today_article = JSON.parse(articleJSON); // 解析JSON字符串为对象 + update(today_article); // 使用解析出的对象更新页面 + } +} + + window.onload = function() { + const savedArticlesDropdown = document.getElementById('saved_articles_dropdown'); + savedArticlesDropdown.addEventListener('change', loadSelectedArticle); + + // 先清空dropdown,以防有多余的选项或重新加载页面时出现重复 + savedArticlesDropdown.innerHTML = ''; + + // 获取localStorage中最后一个(最新)的键值对 + let latestKey, latestValue; + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i); + const value = localStorage.getItem(key); + if (!latestKey) { // 第一次迭代时设置最新文章 + latestKey = key; + latestValue = value; + } + } + + // 首先添加最新保存的文章到下拉菜单 + + if (latestKey && latestValue) { + var latestOption = document.createElement('option'); + latestOption.text = latestKey; + latestOption.value = latestValue; + latestOption.title = latestValue; + savedArticlesDropdown.appendChild(latestOption); + } + + // 接着遍历其余文章并添加到下拉菜单 + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i); + const value = localStorage.getItem(key); + // 确保不重复添加最新文章 + if (key !== latestKey && key !== 'selectedWords') { + var option = document.createElement('option'); + option.text = key; + option.value = value; + option.title = value; + savedArticlesDropdown.appendChild(option); + } + } + + savedArticlesDropdown.selectedIndex = -1; +}