解决了config.js浏览器控制台报错的问题

This commit is contained in:
Alex Yang
2025-11-28 02:25:43 +08:00
parent 2e7d5fb1ce
commit 25a21e284b
3 changed files with 45 additions and 29 deletions

View File

@@ -37,10 +37,18 @@ function initConfigPage() {
async function loadConfig() {
try {
showLoading(true);
const config = await api.getConfig();
populateConfigForm(config);
const result = await api.getConfig();
// 检查API返回的错误
if (result && result.error) {
showErrorMessage('加载配置失败: ' + result.error);
return;
}
populateConfigForm(result);
} catch (error) {
showErrorMessage('加载配置失败: ' + error.message);
// 捕获可能的异常虽然apiRequest不应该再抛出异常
showErrorMessage('加载配置失败: ' + (error.message || '未知错误'));
} finally {
showLoading(false);
}
@@ -67,7 +75,8 @@ function populateConfigForm(config) {
setElementValue('shield-local-rules-file', getSafeValue(shieldConfig.LocalRulesFile, 'data/rules.txt'));
setElementValue('shield-update-interval', getSafeValue(shieldConfig.UpdateInterval, 3600));
setElementValue('shield-hosts-file', getSafeValue(shieldConfig.HostsFile, 'data/hosts.txt'));
setElementValue('shield-block-method', getSafeValue(shieldConfig.BlockMethod, '0.0.0.0'));
// 使用服务器端接受的屏蔽方法值默认使用NXDOMAIN
setElementValue('shield-block-method', getSafeValue(shieldConfig.BlockMethod, 'NXDOMAIN'));
}
// 工具函数:安全设置元素值
@@ -98,10 +107,18 @@ async function handleSaveConfig() {
try {
showLoading(true);
await api.saveConfig(formData);
const result = await api.saveConfig(formData);
// 检查API返回的错误
if (result && result.error) {
showErrorMessage('保存配置失败: ' + result.error);
return;
}
showSuccessMessage('配置保存成功');
} catch (error) {
showErrorMessage('保存配置失败: ' + error.message);
// 捕获可能的异常虽然apiRequest不应该再抛出异常
showErrorMessage('保存配置失败: ' + (error.message || '未知错误'));
} finally {
showLoading(false);
}
@@ -113,10 +130,18 @@ async function handleRestartService() {
try {
showLoading(true);
await api.restartService();
const result = await api.restartService();
// 检查API返回的错误
if (result && result.error) {
showErrorMessage('服务重启失败: ' + result.error);
return;
}
showSuccessMessage('服务重启成功');
} catch (error) {
showErrorMessage('重启服务失败: ' + error.message);
// 捕获可能的异常虽然apiRequest不应该再抛出异常
showErrorMessage('重启服务失败: ' + (error.message || '未知错误'));
} finally {
showLoading(false);
}
@@ -172,7 +197,7 @@ function collectFormData() {
LocalRulesFile: getElementValue('shield-local-rules-file') || './data/rules.txt',
UpdateInterval: updateInterval,
HostsFile: getElementValue('shield-hosts-file') || './data/hosts.txt',
BlockMethod: getElementValue('shield-block-method') || '0.0.0.0'
BlockMethod: getElementValue('shield-block-method') || 'NXDOMAIN'
}
};
}