|
1 | 1 | // Listen for messages from the devtools panel
|
2 | 2 | chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
3 | 3 | if (message.type === "CAPTURE_SCREENSHOT" && message.tabId) {
|
4 |
| - // Get the inspected window's tab |
5 |
| - chrome.tabs.get(message.tabId, (tab) => { |
6 |
| - if (chrome.runtime.lastError) { |
7 |
| - console.error("Error getting tab:", chrome.runtime.lastError); |
8 |
| - sendResponse({ |
9 |
| - success: false, |
10 |
| - error: chrome.runtime.lastError.message, |
11 |
| - }); |
12 |
| - return; |
13 |
| - } |
14 |
| - |
15 |
| - // Get all windows to find the one containing our tab |
16 |
| - chrome.windows.getAll({ populate: true }, (windows) => { |
17 |
| - const targetWindow = windows.find(w => |
18 |
| - w.tabs.some(t => t.id === message.tabId) |
19 |
| - ); |
| 4 | + // First get the server settings |
| 5 | + chrome.storage.local.get(["browserConnectorSettings"], (result) => { |
| 6 | + const settings = result.browserConnectorSettings || { |
| 7 | + serverHost: "localhost", |
| 8 | + serverPort: 3025, |
| 9 | + }; |
20 | 10 |
|
21 |
| - if (!targetWindow) { |
22 |
| - console.error("Could not find window containing the inspected tab"); |
| 11 | + // Get the inspected window's tab |
| 12 | + chrome.tabs.get(message.tabId, (tab) => { |
| 13 | + if (chrome.runtime.lastError) { |
| 14 | + console.error("Error getting tab:", chrome.runtime.lastError); |
23 | 15 | sendResponse({
|
24 | 16 | success: false,
|
25 |
| - error: "Could not find window containing the inspected tab" |
| 17 | + error: chrome.runtime.lastError.message, |
26 | 18 | });
|
27 | 19 | return;
|
28 | 20 | }
|
29 | 21 |
|
30 |
| - // Capture screenshot of the window containing our tab |
31 |
| - chrome.tabs.captureVisibleTab(targetWindow.id, { format: "png" }, (dataUrl) => { |
32 |
| - // Ignore DevTools panel capture error if it occurs |
33 |
| - if (chrome.runtime.lastError && |
34 |
| - !chrome.runtime.lastError.message.includes("devtools://")) { |
35 |
| - console.error("Error capturing screenshot:", chrome.runtime.lastError); |
| 22 | + // Get all windows to find the one containing our tab |
| 23 | + chrome.windows.getAll({ populate: true }, (windows) => { |
| 24 | + const targetWindow = windows.find((w) => |
| 25 | + w.tabs.some((t) => t.id === message.tabId) |
| 26 | + ); |
| 27 | + |
| 28 | + if (!targetWindow) { |
| 29 | + console.error("Could not find window containing the inspected tab"); |
36 | 30 | sendResponse({
|
37 | 31 | success: false,
|
38 |
| - error: chrome.runtime.lastError.message, |
| 32 | + error: "Could not find window containing the inspected tab", |
39 | 33 | });
|
40 | 34 | return;
|
41 | 35 | }
|
42 | 36 |
|
43 |
| - // Send screenshot data to browser connector |
44 |
| - fetch("http://127.0.0.1:3025/screenshot", { |
45 |
| - method: "POST", |
46 |
| - headers: { |
47 |
| - "Content-Type": "application/json", |
48 |
| - }, |
49 |
| - body: JSON.stringify({ |
50 |
| - data: dataUrl, |
51 |
| - path: message.screenshotPath, |
52 |
| - }), |
53 |
| - }) |
54 |
| - .then((response) => response.json()) |
55 |
| - .then((result) => { |
56 |
| - if (result.error) { |
57 |
| - console.error("Error from server:", result.error); |
58 |
| - sendResponse({ success: false, error: result.error }); |
59 |
| - } else { |
60 |
| - console.log("Screenshot saved successfully:", result.path); |
61 |
| - // Send success response even if DevTools capture failed |
| 37 | + // Capture screenshot of the window containing our tab |
| 38 | + chrome.tabs.captureVisibleTab( |
| 39 | + targetWindow.id, |
| 40 | + { format: "png" }, |
| 41 | + (dataUrl) => { |
| 42 | + // Ignore DevTools panel capture error if it occurs |
| 43 | + if ( |
| 44 | + chrome.runtime.lastError && |
| 45 | + !chrome.runtime.lastError.message.includes("devtools://") |
| 46 | + ) { |
| 47 | + console.error( |
| 48 | + "Error capturing screenshot:", |
| 49 | + chrome.runtime.lastError |
| 50 | + ); |
62 | 51 | sendResponse({
|
63 |
| - success: true, |
64 |
| - path: result.path, |
65 |
| - title: tab.title || "Current Tab" |
| 52 | + success: false, |
| 53 | + error: chrome.runtime.lastError.message, |
66 | 54 | });
|
| 55 | + return; |
67 | 56 | }
|
68 |
| - }) |
69 |
| - .catch((error) => { |
70 |
| - console.error("Error sending screenshot data:", error); |
71 |
| - sendResponse({ |
72 |
| - success: false, |
73 |
| - error: error.message || "Failed to save screenshot", |
74 |
| - }); |
75 |
| - }); |
| 57 | + |
| 58 | + // Send screenshot data to browser connector using configured settings |
| 59 | + const serverUrl = `http://${settings.serverHost}:${settings.serverPort}/screenshot`; |
| 60 | + console.log(`Sending screenshot to ${serverUrl}`); |
| 61 | + |
| 62 | + fetch(serverUrl, { |
| 63 | + method: "POST", |
| 64 | + headers: { |
| 65 | + "Content-Type": "application/json", |
| 66 | + }, |
| 67 | + body: JSON.stringify({ |
| 68 | + data: dataUrl, |
| 69 | + path: message.screenshotPath, |
| 70 | + }), |
| 71 | + }) |
| 72 | + .then((response) => response.json()) |
| 73 | + .then((result) => { |
| 74 | + if (result.error) { |
| 75 | + console.error("Error from server:", result.error); |
| 76 | + sendResponse({ success: false, error: result.error }); |
| 77 | + } else { |
| 78 | + console.log("Screenshot saved successfully:", result.path); |
| 79 | + // Send success response even if DevTools capture failed |
| 80 | + sendResponse({ |
| 81 | + success: true, |
| 82 | + path: result.path, |
| 83 | + title: tab.title || "Current Tab", |
| 84 | + }); |
| 85 | + } |
| 86 | + }) |
| 87 | + .catch((error) => { |
| 88 | + console.error("Error sending screenshot data:", error); |
| 89 | + sendResponse({ |
| 90 | + success: false, |
| 91 | + error: error.message || "Failed to save screenshot", |
| 92 | + }); |
| 93 | + }); |
| 94 | + } |
| 95 | + ); |
76 | 96 | });
|
77 | 97 | });
|
78 | 98 | });
|
|
0 commit comments