// 初始化扩展
chrome.runtime.onInstalled.addListener(function() {
  console.log('Cookie提取器已安装');
});

// 监听消息
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if (request.action === 'getCookies') {
    // 接收域名参数
    const url = request.url;
    
    try {
      // 处理URL以获取正确的域名
      const parsedUrl = new URL(url);
      const domain = parsedUrl.hostname;
      
      // 如果是主域名，获取包括所有子域名的cookies
      chrome.cookies.getAll({}, function(allCookies) {
        // 过滤相关域名的cookies
        const filteredCookies = allCookies.filter(cookie => {
          // 检查cookie域是否与当前域或其父域相关
          return cookie.domain === domain || 
                 domain.endsWith(cookie.domain.replace(/^\./, '')) || 
                 cookie.domain.endsWith(domain);
        });
        
        sendResponse({
          success: true, 
          cookies: filteredCookies,
          count: filteredCookies.length
        });
      });
    } catch (e) {
      // 处理URL解析错误
      sendResponse({
        success: false,
        error: e.message
      });
    }
    
    // 返回true以保持消息通道开放，等待异步响应
    return true;
  }
}); 