diff --git a/extension/src/background/index.ts b/extension/src/background/index.ts index 4fea757..6ba46b3 100644 --- a/extension/src/background/index.ts +++ b/extension/src/background/index.ts @@ -9,7 +9,19 @@ const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080/api chrome.runtime.onMessage.addListener((message: { type: string; payload?: any }, _sender: chrome.runtime.MessageSender, sendResponse: (response?: any) => void) => { if (message.type === 'SHOW_INSIGHT') { - console.log('Received tweet data in background:', message.payload); + console.log('[InsightReply Background] Received SHOW_INSIGHT data:', message.payload); + if (_sender.tab?.id) { + console.log(`[InsightReply Background] Forwarding SHOW_INSIGHT to tab ${_sender.tab.id}`); + chrome.tabs.sendMessage(_sender.tab.id, message, (response) => { + if (chrome.runtime.lastError) { + console.error('[InsightReply Background] Error forwarding message to tab:', chrome.runtime.lastError); + } else { + console.log('[InsightReply Background] Successfully forwarded to tab, response:', response); + } + }); + } + sendResponse({ success: true, forwarded: !!_sender.tab?.id }); + return true; } if (message.type === 'FETCH_CUSTOM_STRATEGIES') { diff --git a/extension/src/content/index.ts b/extension/src/content/index.ts index de75472..ec88edb 100644 --- a/extension/src/content/index.ts +++ b/extension/src/content/index.ts @@ -52,7 +52,14 @@ const extractTweetData = (tweetElement: HTMLElement): TweetData | null => { const authorElement = tweetElement.querySelector('[data-testid="User-Name"]'); const linkElement = tweetElement.querySelector('time')?.parentElement as HTMLAnchorElement; - if (!textElement || !authorElement || !linkElement) return null; + if (!textElement || !authorElement || !linkElement) { + console.debug('[InsightReply] Missing elements for tweet extraction:', { + hasText: !!textElement, + hasAuthor: !!authorElement, + hasLink: !!linkElement + }); + return null; + } const getStat = (testid: string) => { const el = tweetElement.querySelector(`[data-testid="${testid}"]`); @@ -146,19 +153,40 @@ const injectInsightButton = (tweetElement: HTMLElement) => { btnContainer.style.marginLeft = '12px'; btnContainer.style.cursor = 'pointer'; - btnContainer.innerHTML = ` -
+ const innerDiv = document.createElement('div'); + innerDiv.style.cssText = 'padding: 4px; border-radius: 9999px; transition: background 0.2s;'; + + // Use event listeners instead of inline handlers to comply with Content Security Policy + innerDiv.addEventListener('mouseover', () => { + innerDiv.style.background = 'rgba(139, 92, 246, 0.1)'; + }); + innerDiv.addEventListener('mouseout', () => { + innerDiv.style.background = 'transparent'; + }); + + innerDiv.innerHTML = ` -
- `; + `; + btnContainer.appendChild(innerDiv); btnContainer.onclick = (e) => { e.stopPropagation(); + e.preventDefault(); + console.log('[InsightReply] Insight button clicked'); + const data = extractTweetData(tweetElement); if (data) { - chrome.runtime.sendMessage({ type: 'SHOW_INSIGHT', payload: data }); + console.log('[InsightReply] Extracted Tweet Data:', data); + chrome.runtime.sendMessage({ type: 'SHOW_INSIGHT', payload: data }, (response) => { + console.log('[InsightReply] Background script responsed SHOW_INSIGHT with:', response); + if (chrome.runtime.lastError) { + console.error('[InsightReply] Error sending SHOW_INSIGHT message:', chrome.runtime.lastError); + } + }); + } else { + console.warn('[InsightReply] Failed to extract tweet data on click'); } }; diff --git a/extension/src/content/sidebar-mount.ts b/extension/src/content/sidebar-mount.ts index ad5efb5..f7e8bd1 100644 --- a/extension/src/content/sidebar-mount.ts +++ b/extension/src/content/sidebar-mount.ts @@ -44,8 +44,11 @@ function initSidebar(tweetData?: any) { } // Listen for messages to show/hide or update data -chrome.runtime.onMessage.addListener((message) => { +chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => { if (message.type === 'SHOW_INSIGHT') { - initSidebar(message.payload) + console.log('[InsightReply Sidebar Mount] Received SHOW_INSIGHT message:', message.payload); + initSidebar(message.payload); + sendResponse({ received: true }); } + return true; });