/** * InsightReply Content Script * 负责解析 X (Twitter) 页面 DOM,提取推文内容并注入交互按钮 */ console.log('InsightReply Content Script Loaded'); // 1. 定义推文数据结构 interface TweetData { id: string; author: string; content: string; stats: { replies: string; retweets: string; likes: string; }; } // 2. 提取推文内容的逻辑 const extractTweetData = (tweetElement: HTMLElement): TweetData | null => { try { // 根据 X 的 DOM 结构提取 (可能会随 Twitter 更新而变化) const textElement = tweetElement.querySelector('[data-testid="tweetText"]'); const authorElement = tweetElement.querySelector('[data-testid="User-Name"]'); const linkElement = tweetElement.querySelector('time')?.parentElement as HTMLAnchorElement; // 互动数据提取 const getStat = (testid: string) => { const el = tweetElement.querySelector(`[data-testid="${testid}"]`); return el?.getAttribute('aria-label') || '0'; }; if (!textElement || !authorElement || !linkElement) return null; const tweetId = linkElement.href.split('/').pop() || ''; return { id: tweetId, author: authorElement.textContent || 'Unknown', content: textElement.textContent || '', stats: { replies: getStat('reply'), retweets: getStat('retweet'), likes: getStat('like'), } }; } catch (e) { console.error('Failed to extract tweet data:', e); return null; } }; // 3. 注入“Insight”按钮 const injectInsightButton = (tweetElement: HTMLElement) => { // 查找操作栏 (Actions bar) const actionBar = tweetElement.querySelector('[role="group"]'); if (!actionBar || actionBar.querySelector('.insight-reply-btn')) return; // 创建按钮 const btnContainer = document.createElement('div'); btnContainer.className = 'insight-reply-btn'; btnContainer.style.display = 'flex'; btnContainer.style.alignItems = 'center'; btnContainer.style.marginLeft = '12px'; btnContainer.style.cursor = 'pointer'; // 按钮内部图标 (简易版) btnContainer.innerHTML = `