// DNS查询页面功能实现 // 初始化查询页面 function initQueryPage() { console.log('初始化DNS查询页面...'); setupQueryEventListeners(); loadQueryHistory(); } // 执行DNS查询 async function handleDNSQuery() { const domainInput = document.getElementById('dns-query-domain'); const resultDiv = document.getElementById('query-result'); if (!domainInput || !resultDiv) { console.error('找不到必要的DOM元素'); return; } const domain = domainInput.value.trim(); if (!domain) { showErrorMessage('请输入域名'); return; } try { const response = await fetch(`/api/query?domain=${encodeURIComponent(domain)}`); if (!response.ok) { throw new Error('查询失败'); } const result = await response.json(); displayQueryResult(result, domain); saveQueryHistory(domain, result); loadQueryHistory(); } catch (error) { console.error('DNS查询出错:', error); showErrorMessage('查询失败,请稍后重试'); } } // 显示查询结果 function displayQueryResult(result, domain) { const resultDiv = document.getElementById('query-result'); if (!resultDiv) return; // 显示结果容器 resultDiv.classList.remove('hidden'); // 解析结果 const status = result.blocked ? '被屏蔽' : '正常'; const statusClass = result.blocked ? 'text-danger' : 'text-success'; const blockType = result.blocked ? result.blockRuleType || '未知' : '正常'; const blockRule = result.blocked ? result.blockRule || '未知' : '无'; const blockSource = result.blocked ? result.blocksource || '未知' : '无'; const timestamp = new Date(result.timestamp).toLocaleString(); // 更新结果显示 document.getElementById('result-domain').textContent = domain; document.getElementById('result-status').innerHTML = `${status}`; document.getElementById('result-type').textContent = blockType; // 检查是否存在屏蔽规则显示元素,如果不存在则创建 let blockRuleElement = document.getElementById('result-block-rule'); if (!blockRuleElement) { // 创建屏蔽规则显示区域 const grid = resultDiv.querySelector('.grid'); if (grid) { const newGridItem = document.createElement('div'); newGridItem.className = 'bg-gray-50 p-4 rounded-lg'; newGridItem.innerHTML = `
-
`; grid.appendChild(newGridItem); blockRuleElement = document.getElementById('result-block-rule'); } } // 更新屏蔽规则显示 if (blockRuleElement) { blockRuleElement.textContent = blockRule; } // 检查是否存在屏蔽来源显示元素,如果不存在则创建 let blockSourceElement = document.getElementById('result-block-source'); if (!blockSourceElement) { // 创建屏蔽来源显示区域 const grid = resultDiv.querySelector('.grid'); if (grid) { const newGridItem = document.createElement('div'); newGridItem.className = 'bg-gray-50 p-4 rounded-lg'; newGridItem.innerHTML = `-
`; grid.appendChild(newGridItem); blockSourceElement = document.getElementById('result-block-source'); } } // 更新屏蔽来源显示 if (blockSourceElement) { blockSourceElement.textContent = blockSource; } document.getElementById('result-time').textContent = timestamp; document.getElementById('result-details').textContent = JSON.stringify(result, null, 2); } // 保存查询历史 function saveQueryHistory(domain, result) { // 获取现有历史记录 let history = JSON.parse(localStorage.getItem('dnsQueryHistory') || '[]'); // 创建历史记录项 const historyItem = { domain: domain, timestamp: new Date().toISOString(), result: { blocked: result.blocked, blockRuleType: result.blockRuleType, blockRule: result.blockRule, blocksource: result.blocksource } }; // 添加到历史记录开头 history.unshift(historyItem); // 限制历史记录数量 if (history.length > 20) { history = history.slice(0, 20); } // 保存到本地存储 localStorage.setItem('dnsQueryHistory', JSON.stringify(history)); } // 加载查询历史 function loadQueryHistory() { const historyDiv = document.getElementById('query-history'); if (!historyDiv) return; // 获取历史记录 const history = JSON.parse(localStorage.getItem('dnsQueryHistory') || '[]'); if (history.length === 0) { historyDiv.innerHTML = '