2025-02-19 09:56:59 -06:00
|
|
|
// Listen for messages from the devtools panel
|
|
|
|
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|
|
|
if (message.type === "CAPTURE_SCREENSHOT" && message.tabId) {
|
2025-03-01 21:08:24 -05:00
|
|
|
// First get the server settings
|
|
|
|
chrome.storage.local.get(["browserConnectorSettings"], (result) => {
|
|
|
|
const settings = result.browserConnectorSettings || {
|
|
|
|
serverHost: "localhost",
|
|
|
|
serverPort: 3025,
|
|
|
|
};
|
2025-02-19 09:56:59 -06:00
|
|
|
|
2025-03-01 21:08:24 -05:00
|
|
|
// Get the inspected window's tab
|
|
|
|
chrome.tabs.get(message.tabId, (tab) => {
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
console.error("Error getting tab:", chrome.runtime.lastError);
|
2025-02-19 09:56:59 -06:00
|
|
|
sendResponse({
|
|
|
|
success: false,
|
2025-03-01 21:08:24 -05:00
|
|
|
error: chrome.runtime.lastError.message,
|
2025-02-19 09:56:59 -06:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-03-01 21:08:24 -05:00
|
|
|
// Get all windows to find the one containing our tab
|
|
|
|
chrome.windows.getAll({ populate: true }, (windows) => {
|
|
|
|
const targetWindow = windows.find((w) =>
|
|
|
|
w.tabs.some((t) => t.id === message.tabId)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!targetWindow) {
|
|
|
|
console.error("Could not find window containing the inspected tab");
|
2025-02-19 09:56:59 -06:00
|
|
|
sendResponse({
|
|
|
|
success: false,
|
2025-03-01 21:08:24 -05:00
|
|
|
error: "Could not find window containing the inspected tab",
|
2025-02-19 09:56:59 -06:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-03-01 21:08:24 -05:00
|
|
|
// 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://")
|
|
|
|
) {
|
|
|
|
console.error(
|
|
|
|
"Error capturing screenshot:",
|
|
|
|
chrome.runtime.lastError
|
|
|
|
);
|
2025-02-19 09:56:59 -06:00
|
|
|
sendResponse({
|
2025-03-01 21:08:24 -05:00
|
|
|
success: false,
|
|
|
|
error: chrome.runtime.lastError.message,
|
2025-02-19 09:56:59 -06:00
|
|
|
});
|
2025-03-01 21:08:24 -05:00
|
|
|
return;
|
2025-02-19 09:56:59 -06:00
|
|
|
}
|
2025-03-01 21:08:24 -05:00
|
|
|
|
|
|
|
// Send screenshot data to browser connector using configured settings
|
|
|
|
const serverUrl = `http://${settings.serverHost}:${settings.serverPort}/screenshot`;
|
|
|
|
console.log(`Sending screenshot to ${serverUrl}`);
|
|
|
|
|
|
|
|
fetch(serverUrl, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
data: dataUrl,
|
|
|
|
path: message.screenshotPath,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((result) => {
|
|
|
|
if (result.error) {
|
|
|
|
console.error("Error from server:", result.error);
|
|
|
|
sendResponse({ success: false, error: result.error });
|
|
|
|
} else {
|
|
|
|
console.log("Screenshot saved successfully:", result.path);
|
|
|
|
// Send success response even if DevTools capture failed
|
|
|
|
sendResponse({
|
|
|
|
success: true,
|
|
|
|
path: result.path,
|
|
|
|
title: tab.title || "Current Tab",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error("Error sending screenshot data:", error);
|
|
|
|
sendResponse({
|
|
|
|
success: false,
|
|
|
|
error: error.message || "Failed to save screenshot",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2025-02-19 09:56:59 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return true; // Required to use sendResponse asynchronously
|
|
|
|
}
|
|
|
|
});
|