diff --git a/chrome-extension/background.js b/chrome-extension/background.js index 45bc4d7..0faf4a6 100644 --- a/chrome-extension/background.js +++ b/chrome-extension/background.js @@ -339,6 +339,42 @@ async function retestConnectionOnRefresh(tabId) { }); } +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.type === "DELEGATE_SCREENSHOT" && message.tabId) { + // Get the tab we want to capture + chrome.tabs.get(message.tabId, (tab) => { + if (chrome.runtime.lastError) { + sendResponse({ success: false, error: chrome.runtime.lastError.message }); + return; + } + + // Get the window containing this tab + chrome.windows.get(tab.windowId, { populate: true }, (window) => { + // Make sure the tab is active in its window + chrome.tabs.update(message.tabId, { active: true }, () => { + // Focus the window + chrome.windows.update(tab.windowId, { focused: true }, () => { + // Wait a moment for the window to focus + setTimeout(() => { + // Capture the visible tab in that window + chrome.tabs.captureVisibleTab(tab.windowId, { format: "png" }, (dataUrl) => { + if (chrome.runtime.lastError) { + sendResponse({ success: false, error: chrome.runtime.lastError.message }); + return; + } + + sendResponse({ success: true, dataUrl: dataUrl }); + }); + }, 100); + }); + }); + }); + }); + + return true; // Keep the message channel open for the async response + } +}); + // Function to capture and send screenshot function captureAndSendScreenshot(message, settings, sendResponse) { // Get the inspected window's tab @@ -352,6 +388,16 @@ function captureAndSendScreenshot(message, settings, sendResponse) { return; } + // Skip DevTools tabs + if (tab.url && tab.url.startsWith('devtools://')) { + console.error("Cannot capture DevTools tab:", tab.url); + sendResponse({ + success: false, + error: "Cannot capture DevTools tabs. Please capture the actual application tab.", + }); + return; + } + // Get all windows to find the one containing our tab chrome.windows.getAll({ populate: true }, (windows) => { const targetWindow = windows.find((w) => @@ -367,16 +413,19 @@ function captureAndSendScreenshot(message, settings, sendResponse) { return; } + console.log(`Capturing screenshot of window ${targetWindow.id} containing tab ${message.tabId}`); + + // First focus the window and activate the tab + chrome.windows.update(targetWindow.id, { focused: true }, () => { + chrome.tabs.update(message.tabId, { active: true }, () => { + // Small delay to ensure the window and tab are focused + setTimeout(() => { // Capture screenshot of the window containing our tab chrome.tabs.captureVisibleTab( targetWindow.id, { format: "png" }, (dataUrl) => { - // Ignore DevTools panel capture error if it occurs - if ( - chrome.runtime.lastError && - !chrome.runtime.lastError.message.includes("devtools://") - ) { + if (chrome.runtime.lastError) { console.error( "Error capturing screenshot:", chrome.runtime.lastError @@ -409,7 +458,7 @@ function captureAndSendScreenshot(message, settings, sendResponse) { sendResponse({ success: false, error: result.error }); } else { console.log("Screenshot saved successfully:", result.path); - // Send success response even if DevTools capture failed + // Send success response sendResponse({ success: true, path: result.path, @@ -426,6 +475,9 @@ function captureAndSendScreenshot(message, settings, sendResponse) { }); } ); + }, 100); // 100ms delay + }); + }); }); }); } diff --git a/chrome-extension/devtools.js b/chrome-extension/devtools.js index 6197f2f..6aeb269 100644 --- a/chrome-extension/devtools.js +++ b/chrome-extension/devtools.js @@ -965,42 +965,40 @@ async function setupWebSocket() { // Uncomment the next line for debug purposes only // console.log("Chrome Extension: Received heartbeat response"); } else if (message.type === "take-screenshot") { - console.log("Chrome Extension: Taking screenshot..."); - // Capture screenshot of the current tab - chrome.tabs.captureVisibleTab(null, { format: "png" }, (dataUrl) => { + console.log("DevTools: Delegating screenshot capture to background script"); + + // Send message to background script to handle the screenshot + chrome.runtime.sendMessage({ + type: "DELEGATE_SCREENSHOT", + tabId: chrome.devtools.inspectedWindow.tabId, + requestId: message.requestId + }, (response) => { if (chrome.runtime.lastError) { - console.error( - "Chrome Extension: Screenshot capture failed:", - chrome.runtime.lastError - ); - ws.send( - JSON.stringify({ - type: "screenshot-error", - error: chrome.runtime.lastError.message, - requestId: message.requestId, - }) - ); + console.error("Error delegating screenshot:", chrome.runtime.lastError); + ws.send(JSON.stringify({ + type: "screenshot-error", + error: chrome.runtime.lastError.message, + requestId: message.requestId + })); return; } - - console.log("Chrome Extension: Screenshot captured successfully"); - // Just send the screenshot data, let the server handle paths - const response = { - type: "screenshot-data", - data: dataUrl, - requestId: message.requestId, - // Only include path if it's configured in settings - ...(settings.screenshotPath && { path: settings.screenshotPath }), - // Include auto-paste setting - autoPaste: settings.allowAutoPaste, - }; - - console.log("Chrome Extension: Sending screenshot data response", { - ...response, - data: "[base64 data]", - }); - - ws.send(JSON.stringify(response)); + + if (response && response.success) { + // Forward the response from the background script + ws.send(JSON.stringify({ + type: "screenshot-data", + data: response.dataUrl, + requestId: message.requestId, + ...(settings.screenshotPath && { path: settings.screenshotPath }), + autoPaste: settings.allowAutoPaste + })); + } else { + ws.send(JSON.stringify({ + type: "screenshot-error", + error: response?.error || "Unknown error capturing screenshot", + requestId: message.requestId + })); + } }); } else if (message.type === "get-current-url") { console.log("Chrome Extension: Received request for current URL");