diff --git a/worker.js b/worker.js index 282db93..d3708d2 100644 --- a/worker.js +++ b/worker.js @@ -1452,7 +1452,18 @@ async function toggleCategoryState(categoryIndex) { 新闻 - + + + + 导出 + + + + + + 导入 + + 退出 @@ -1653,32 +1664,32 @@ async function toggleCategoryState(categoryIndex) { document.getElementById('editCategoryModal').style.display = "flex"; } // 添加编辑分类表单提交事件 -document.getElementById('editCategoryForm')?.addEventListener('submit', async function(event) { - event.preventDefault(); - const formData = new FormData(event.target); - const data = { - categoryIndex: parseInt(formData.get('categoryIndex')), - newName: formData.get('categoryName') - }; - - const response = await fetch('/edit-category', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(data) - }); - - if (response.ok) { - closeModal('editCategoryModal'); - // 更新分类名称显示 - const categoryNameElement = document.getElementById(\`category-name-\${data.categoryIndex}\`); - if (categoryNameElement) { - categoryNameElement.textContent = data.newName; - } - } else { - const error = await response.json(); - alert(error.error || '修改失败'); - } -}); + document.getElementById('editCategoryForm')?.addEventListener('submit', async function(event) { + event.preventDefault(); + const formData = new FormData(event.target); + const data = { + categoryIndex: parseInt(formData.get('categoryIndex')), + newName: formData.get('categoryName') + }; + + const response = await fetch('/edit-category', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(data) + }); + + if (response.ok) { + closeModal('editCategoryModal'); + // 更新分类名称显示 + const categoryNameElement = document.getElementById(\`category-name-\${data.categoryIndex}\`); + if (categoryNameElement) { + categoryNameElement.textContent = data.newName; + } + } else { + const error = await response.json(); + alert(error.error || '修改失败'); + } + }); // 绑定确认对话框按钮事件 document.getElementById('confirmOk').addEventListener('click', function() { @@ -2022,7 +2033,32 @@ document.getElementById('editCategoryForm')?.addEventListener('submit', async fu } } } - + // 导出数据 + async function exportData() { + // 直接跳转到导出接口,浏览器会自动下载 + window.location.href = '/export'; + } + + // 导入数据 + async function importData(file) { + if (!file) return; + + const formData = new FormData(); + formData.append('file', file); + + const response = await fetch('/import', { + method: 'POST', + body: formData, + }); + + const result = await response.json(); + if (response.ok) { + alert('导入成功,页面即将刷新'); + location.reload(); + } else { + alert('导入失败:' + (result.error || '未知错误')); + } + } // 高亮文本函数 function highlightText(element, term) { const text = element.textContent; @@ -2033,9 +2069,9 @@ document.getElementById('editCategoryForm')?.addEventListener('submit', async fu } // 转义正则表达式特殊字符 -function escapeRegExp(string) { - return string.replace(/[.*+?^$()|[\]\\]/g, '\\$&'); -} + function escapeRegExp(string) { + return string.replace(/[.*+?^$()|[\]\\]/g, '\\$&'); + } // 初始化 window.addEventListener('load', async function() {