1
0
Fork 0

Fix bug 540

Bug540-XiongJiaming
熊佳明 2024-05-26 20:41:16 +08:00
parent 175e086a78
commit 0181dc4131
1 changed files with 73 additions and 21 deletions

View File

@ -293,29 +293,81 @@
} }
} }
function saveArticle() { function saveArticle() {
const articleTitle = document.getElementById('article_title').innerText; // 获取文章标题 const article = {
const article = document.getElementById('article').innerText; // 获取文章内容 user_level: document.getElementById('user_level').innerText,
const savedArticlesDropdown = document.getElementById('saved_articles_dropdown'); // 获取下拉菜单 text_level: document.getElementById('text_level').innerText,
var option = document.createElement('option'); // 创建一个新的下拉菜单选项 date: document.getElementById('date').innerText.replace('Article added on: ', ''),
option.text = articleTitle; // 将文章标题作为选项文本 article_title: document.getElementById('article_title').innerText,
option.value = article; // 将文章内容作为选项值 article_body: document.getElementById('article').innerText,
option.title = article; // 将文章内容作为工具提示内容 source: document.getElementById('source').innerText,
savedArticlesDropdown.appendChild(option); // 将选项添加到下拉菜单中 question: document.getElementById('question').innerText,
localStorage.setItem(articleTitle, article) //将标记文章存储到localstorage中 answer: document.getElementById('answer').innerText
alert("文章已标记") };
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() { window.onload = function() {
const savedArticlesDropdown = document.getElementById('saved_articles_dropdown'); 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++) { for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i); // 获取localStorage中的键 const key = localStorage.key(i);
const value = localStorage.getItem(key); // 获取localStorage中的值 const value = localStorage.getItem(key);
// 创建一个新的下拉菜单选项 if (!latestKey) { // 第一次迭代时设置最新文章
var option = document.createElement('option'); latestKey = key;
option.text = key; // 将文章标题作为选项文本 latestValue = value;
option.value = value; // 将文章内容作为选项值
option.title = value; // 将文章内容作为工具提示内容
savedArticlesDropdown.appendChild(option); // 将选项添加到下拉菜单中
} }
}
// 首先添加最新保存的文章到下拉菜单
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) {
var option = document.createElement('option');
option.text = key;
option.value = value;
option.title = value;
savedArticlesDropdown.appendChild(option);
}
}
savedArticlesDropdown.selectedIndex = -1;
} }
</script> </script>
</body> </body>