更新 worker.js
增加一键导出/导入功能
This commit is contained in:
98
worker.js
98
worker.js
@@ -1452,7 +1452,18 @@ async function toggleCategoryState(categoryIndex) {
|
||||
<span class="iconify" data-icon="mdi:newspaper" data-width="16px" data-height="16px"></span>
|
||||
新闻
|
||||
</button>
|
||||
|
||||
<!-- 导出按钮 -->
|
||||
<button onclick="exportData()" style="background-color: #17a2b8; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer; display: flex; align-items: center; gap: 5px; white-space: nowrap;">
|
||||
<span class="iconify" data-icon="mdi:export" data-width="16px" data-height="16px"></span>
|
||||
导出
|
||||
</button>
|
||||
|
||||
<!-- 导入按钮(触发隐藏 file input) -->
|
||||
<button onclick="document.getElementById('importFile').click()" style="background-color: #6c757d; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer; display: flex; align-items: center; gap: 5px; white-space: nowrap;">
|
||||
<span class="iconify" data-icon="mdi:import" data-width="16px" data-height="16px"></span>
|
||||
导入
|
||||
</button>
|
||||
<input type="file" id="importFile" accept=".json,application/json" style="display: none;" onchange="importData(this.files[0])">
|
||||
<button onclick="location.href='/login.html'" style="background-color: #007BFF; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer; display: flex; align-items: center; gap: 5px; white-space: nowrap;">
|
||||
<span class="iconify" data-icon="mdi:logout" data-width="16px" data-height="16px"></span>
|
||||
退出
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user