编辑代码

// 初始化配置
chrome.runtime.onInstalled.addListener(() => {
  chrome.storage.local.set({
    keywords: ['紧急通知', '系统维护'],
    interval: 30
  });
});

// 定时检测
chrome.alarms.create('checkKeywords', { periodInMinutes: 1 });

chrome.alarms.onAlarm.addListener((alarm) => {
  if (alarm.name === 'checkKeywords') {
    checkCurrentPage();
  }
});

async function checkCurrentPage() {
  const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
  
  if (!tab.url) return;

  try {
    const result = await chrome.tabs.executeScript(tab.id, {
      code: `document.body.innerText`
    });
    
    const { keywords } = await chrome.storage.local.get('keywords');
    const found = keywords.filter(kw => 
      result[0].toLowerCase().includes(kw.toLowerCase())
    );
    
    if (found.length > 0) {
      showNotification(found);
    }
  } catch (error) {
    console.error('检测失败:', error);
  }
}

function showNotification(keywords) {
  chrome.notifications.create({
    type: 'basic',
    iconUrl: 'icons/icon48.png',
    title: '发现匹配关键词!',
    message: `检测到关键词:${keywords.join(', ')}`
  });
}