document.addEventListener('DOMContentLoaded', function() { const refreshBtn = document.getElementById('refresh'); const clearBtn = document.getElementById('clear'); const exportBtn = document.getElementById('export'); const webhookInput = document.getElementById('webhookUrl'); const testWebhookBtn = document.getElementById('testWebhook'); const webhookStatus = document.getElementById('webhookStatus'); const statusInfo = document.getElementById('statusInfo'); const serviceCards = document.getElementById('serviceCards'); const emptyState = document.getElementById('emptyState'); // 服务配置 const SERVICES = { douyin: { name: 'douyin', displayName: '抖音', icon: '🎵' } }; // 加载Webhook配置 function loadWebhookConfig() { chrome.storage.local.get(['webhookUrl'], function(result) { if (result.webhookUrl) { webhookInput.value = result.webhookUrl; } updateTestButtonState(); }); } // 保存Webhook配置 function saveWebhookConfig() { const url = webhookInput.value.trim(); chrome.storage.local.set({ webhookUrl: url }); showStatusInfo('Webhook地址已保存'); updateTestButtonState(); } // 更新测试按钮状态 function updateTestButtonState() { const url = webhookInput.value.trim(); testWebhookBtn.disabled = !url || !isValidUrl(url); } // 验证URL格式 function isValidUrl(string) { try { new URL(string); return string.startsWith('http://') || string.startsWith('https://'); } catch (_) { return false; } } // 测试Webhook回调 async function testWebhook() { const url = webhookInput.value.trim(); if (!url) { webhookStatus.textContent = '请先输入Webhook地址'; webhookStatus.style.color = '#dc3545'; return; } testWebhookBtn.disabled = true; testWebhookBtn.textContent = '⏳ 测试中...'; webhookStatus.textContent = '正在发送测试请求...'; webhookStatus.style.color = '#17a2b8'; // 获取现有数据或创建测试数据 chrome.storage.local.get(['cookieData_douyin'], async function(result) { let testData; if (result.cookieData_douyin) { // 使用现有数据 testData = { service: 'douyin', cookie: result.cookieData_douyin.cookie, timestamp: new Date().toISOString(), test: true, message: '这是一个测试回调,使用了真实的Cookie数据' }; } else { // 使用模拟数据 testData = { service: 'douyin', cookie: 'test_cookie=test_value; another_cookie=another_value', timestamp: new Date().toISOString(), test: true, message: '这是一个测试回调,使用了模拟Cookie数据' }; } try { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(testData) }); if (response.ok) { webhookStatus.textContent = `✅ 测试成功 (${response.status})`; webhookStatus.style.color = '#28a745'; } else { webhookStatus.textContent = `❌ 服务器错误 (${response.status})`; webhookStatus.style.color = '#dc3545'; } } catch (error) { console.error('Webhook测试失败:', error); if (error.name === 'TypeError' && error.message.includes('fetch')) { webhookStatus.textContent = '❌ 网络错误或跨域限制'; } else { webhookStatus.textContent = `❌ 请求失败: ${error.message}`; } webhookStatus.style.color = '#dc3545'; } finally { testWebhookBtn.disabled = false; testWebhookBtn.textContent = '🔧 测试'; updateTestButtonState(); // 5秒后清除状态信息 setTimeout(() => { webhookStatus.textContent = ''; }, 5000); } }); } // 显示状态信息 function showStatusInfo(message) { statusInfo.textContent = message; statusInfo.style.display = 'block'; setTimeout(() => { statusInfo.style.display = 'none'; }, 3000); } // 加载服务数据 function loadServiceData() { const serviceKeys = Object.keys(SERVICES).map(service => `cookieData_${service}`); chrome.storage.local.get(serviceKeys, function(result) { const hasData = Object.keys(result).length > 0; if (!hasData) { serviceCards.innerHTML = ''; emptyState.style.display = 'block'; return; } emptyState.style.display = 'none'; serviceCards.innerHTML = ''; Object.keys(SERVICES).forEach(serviceKey => { const service = SERVICES[serviceKey]; const data = result[`cookieData_${serviceKey}`]; if (data) { createServiceCard(service, data); } }); }); } // 创建服务卡片 function createServiceCard(service, data) { const card = document.createElement('div'); card.className = 'service-card'; const isRecent = Date.now() - data.timestamp < 5 * 60 * 1000; // 5分钟内 const lastUpdate = new Date(data.lastUpdate).toLocaleString(); card.innerHTML = `