feat: CSP Violation ( 感叹号图标被拦截 )
All checks were successful
Extension Build & Release / build (push) Successful in 48s

This commit is contained in:
zs
2026-03-03 00:19:24 +08:00
parent dc808628d3
commit 50bd2925c1
3 changed files with 52 additions and 9 deletions

View File

@@ -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) => { chrome.runtime.onMessage.addListener((message: { type: string; payload?: any }, _sender: chrome.runtime.MessageSender, sendResponse: (response?: any) => void) => {
if (message.type === 'SHOW_INSIGHT') { 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') { if (message.type === 'FETCH_CUSTOM_STRATEGIES') {

View File

@@ -52,7 +52,14 @@ const extractTweetData = (tweetElement: HTMLElement): TweetData | null => {
const authorElement = tweetElement.querySelector('[data-testid="User-Name"]'); const authorElement = tweetElement.querySelector('[data-testid="User-Name"]');
const linkElement = tweetElement.querySelector('time')?.parentElement as HTMLAnchorElement; 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 getStat = (testid: string) => {
const el = tweetElement.querySelector(`[data-testid="${testid}"]`); const el = tweetElement.querySelector(`[data-testid="${testid}"]`);
@@ -146,19 +153,40 @@ const injectInsightButton = (tweetElement: HTMLElement) => {
btnContainer.style.marginLeft = '12px'; btnContainer.style.marginLeft = '12px';
btnContainer.style.cursor = 'pointer'; btnContainer.style.cursor = 'pointer';
btnContainer.innerHTML = ` const innerDiv = document.createElement('div');
<div style="padding: 4px; border-radius: 9999px; transition: background 0.2s;" onmouseover="this.style.background='rgba(139, 92, 246, 0.1)'" onmouseout="this.style.background='transparent'"> 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 = `
<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor" style="color: #8B5CF6;"> <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor" style="color: #8B5CF6;">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path> <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path>
</svg> </svg>
</div>
`; `;
btnContainer.appendChild(innerDiv);
btnContainer.onclick = (e) => { btnContainer.onclick = (e) => {
e.stopPropagation(); e.stopPropagation();
e.preventDefault();
console.log('[InsightReply] Insight button clicked');
const data = extractTweetData(tweetElement); const data = extractTweetData(tweetElement);
if (data) { 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');
} }
}; };

View File

@@ -44,8 +44,11 @@ function initSidebar(tweetData?: any) {
} }
// Listen for messages to show/hide or update data // 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') { 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;
}); });