230 lines
7.4 KiB
JavaScript
230 lines
7.4 KiB
JavaScript
// 初始化远程黑名单面板
|
|
function initBlacklistsPanel() {
|
|
// 加载远程黑名单列表
|
|
loadBlacklists();
|
|
|
|
// 初始化事件监听器
|
|
initBlacklistsEventListeners();
|
|
}
|
|
|
|
// 初始化事件监听器
|
|
function initBlacklistsEventListeners() {
|
|
// 添加黑名单按钮
|
|
document.getElementById('add-blacklist').addEventListener('click', addBlacklist);
|
|
|
|
// 更新所有黑名单按钮
|
|
document.getElementById('update-all-blacklists').addEventListener('click', updateAllBlacklists);
|
|
|
|
// 按Enter键添加黑名单
|
|
document.getElementById('blacklist-url').addEventListener('keypress', function(e) {
|
|
if (e.key === 'Enter') {
|
|
addBlacklist();
|
|
}
|
|
});
|
|
}
|
|
|
|
// 加载远程黑名单列表
|
|
function loadBlacklists() {
|
|
const tbody = document.getElementById('blacklists-table').querySelector('tbody');
|
|
showLoading(tbody);
|
|
|
|
apiRequest('/api/shield/blacklists')
|
|
.then(data => {
|
|
renderBlacklists(data);
|
|
})
|
|
.catch(error => {
|
|
console.error('获取远程黑名单列表失败:', error);
|
|
showError(tbody, '获取远程黑名单列表失败');
|
|
window.showNotification('获取远程黑名单列表失败', 'error');
|
|
});
|
|
}
|
|
|
|
// 渲染远程黑名单表格
|
|
function renderBlacklists(blacklists) {
|
|
const tbody = document.getElementById('blacklists-table').querySelector('tbody');
|
|
if (!tbody) return;
|
|
|
|
if (!blacklists || blacklists.length === 0) {
|
|
showEmpty(tbody, '暂无远程黑名单');
|
|
return;
|
|
}
|
|
|
|
tbody.innerHTML = '';
|
|
|
|
blacklists.forEach(list => {
|
|
addBlacklistToTable(list);
|
|
});
|
|
|
|
// 初始化表格排序
|
|
initTableSort('blacklists-table');
|
|
|
|
// 初始化操作按钮监听器
|
|
initBlacklistsActionListeners();
|
|
}
|
|
|
|
// 添加黑名单到表格
|
|
function addBlacklistToTable(list) {
|
|
const tbody = document.getElementById('blacklists-table').querySelector('tbody');
|
|
const row = document.createElement('tr');
|
|
|
|
const statusClass = list.status === 'success' ? 'status-success' :
|
|
list.status === 'error' ? 'status-error' : 'status-pending';
|
|
|
|
const statusText = list.status === 'success' ? '正常' :
|
|
list.status === 'error' ? '错误' : '等待中';
|
|
|
|
const lastUpdate = list.lastUpdate ? new Date(list.lastUpdate).toLocaleString() : '从未';
|
|
|
|
row.innerHTML = `
|
|
<td>${list.name}</td>
|
|
<td>${list.url}</td>
|
|
<td>
|
|
<span class="status-badge ${statusClass}">${statusText}</span>
|
|
</td>
|
|
<td>${list.rulesCount || 0}</td>
|
|
<td>${lastUpdate}</td>
|
|
<td class="actions-cell">
|
|
<button class="btn btn-primary btn-sm update-blacklist" data-id="${list.id}">
|
|
<i class="fas fa-sync-alt"></i> 更新
|
|
</button>
|
|
<button class="btn btn-danger btn-sm delete-blacklist" data-id="${list.id}">
|
|
<i class="fas fa-trash-alt"></i> 删除
|
|
</button>
|
|
</td>
|
|
`;
|
|
|
|
tbody.appendChild(row);
|
|
}
|
|
|
|
// 添加远程黑名单
|
|
function addBlacklist() {
|
|
const nameInput = document.getElementById('blacklist-name');
|
|
const urlInput = document.getElementById('blacklist-url');
|
|
|
|
const name = nameInput.value.trim();
|
|
const url = urlInput.value.trim();
|
|
|
|
if (!name) {
|
|
window.showNotification('请输入黑名单名称', 'warning');
|
|
nameInput.focus();
|
|
return;
|
|
}
|
|
|
|
if (!url) {
|
|
window.showNotification('请输入黑名单URL', 'warning');
|
|
urlInput.focus();
|
|
return;
|
|
}
|
|
|
|
// 简单的URL格式验证
|
|
if (!isValidUrl(url)) {
|
|
window.showNotification('请输入有效的URL', 'warning');
|
|
urlInput.focus();
|
|
return;
|
|
}
|
|
|
|
apiRequest('/api/shield/blacklists', 'POST', { name: name, url: url })
|
|
.then(data => {
|
|
if (data.status === 'success') {
|
|
window.showNotification('远程黑名单添加成功', 'success');
|
|
nameInput.value = '';
|
|
urlInput.value = '';
|
|
loadBlacklists();
|
|
} else {
|
|
window.showNotification(`添加失败: ${data.message || '未知错误'}`, 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('添加远程黑名单失败:', error);
|
|
window.showNotification('添加远程黑名单失败', 'error');
|
|
});
|
|
}
|
|
|
|
// 更新远程黑名单
|
|
function updateBlacklist(id) {
|
|
apiRequest(`/api/shield/blacklists/${id}/update`, 'POST')
|
|
.then(data => {
|
|
if (data.status === 'success') {
|
|
window.showNotification('远程黑名单更新成功', 'success');
|
|
loadBlacklists();
|
|
} else {
|
|
window.showNotification(`更新失败: ${data.message || '未知错误'}`, 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('更新远程黑名单失败:', error);
|
|
window.showNotification('更新远程黑名单失败', 'error');
|
|
});
|
|
}
|
|
|
|
// 更新所有远程黑名单
|
|
function updateAllBlacklists() {
|
|
confirmAction(
|
|
'确定要更新所有远程黑名单吗?这可能需要一些时间。',
|
|
() => {
|
|
apiRequest('/api/shield/blacklists', 'PUT')
|
|
.then(data => {
|
|
if (data.status === 'success') {
|
|
window.showNotification('所有远程黑名单更新成功', 'success');
|
|
loadBlacklists();
|
|
} else {
|
|
window.showNotification(`更新失败: ${data.message || '未知错误'}`, 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('更新所有远程黑名单失败:', error);
|
|
window.showNotification('更新所有远程黑名单失败', 'error');
|
|
});
|
|
}
|
|
);
|
|
}
|
|
|
|
// 删除远程黑名单
|
|
function deleteBlacklist(id) {
|
|
apiRequest(`/api/shield/blacklists/${id}`, 'DELETE')
|
|
.then(data => {
|
|
if (data.status === 'success') {
|
|
window.showNotification('远程黑名单删除成功', 'success');
|
|
loadBlacklists();
|
|
} else {
|
|
window.showNotification(`删除失败: ${data.message || '未知错误'}`, 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('删除远程黑名单失败:', error);
|
|
window.showNotification('删除远程黑名单失败', 'error');
|
|
});
|
|
}
|
|
|
|
// 为操作按钮添加事件监听器
|
|
function initBlacklistsActionListeners() {
|
|
// 更新按钮
|
|
document.querySelectorAll('.update-blacklist').forEach(button => {
|
|
button.addEventListener('click', function() {
|
|
const id = this.getAttribute('data-id');
|
|
updateBlacklist(id);
|
|
});
|
|
});
|
|
|
|
// 删除按钮
|
|
document.querySelectorAll('.delete-blacklist').forEach(button => {
|
|
button.addEventListener('click', function() {
|
|
const id = this.getAttribute('data-id');
|
|
|
|
confirmAction(
|
|
'确定要删除这条远程黑名单吗?',
|
|
() => deleteBlacklist(id)
|
|
);
|
|
});
|
|
});
|
|
}
|
|
|
|
// 验证URL格式
|
|
function isValidUrl(url) {
|
|
try {
|
|
new URL(url);
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
} |