@@ -8,94 +8,150 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
8
8
serverPort : 3025 ,
9
9
} ;
10
10
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 ) ;
11
+ // Validate server identity first
12
+ validateServerIdentity ( settings . serverHost , settings . serverPort )
13
+ . then ( ( isValid ) => {
14
+ if ( ! isValid ) {
15
+ console . error (
16
+ "Cannot capture screenshot: Not connected to a valid browser tools server"
17
+ ) ;
18
+ sendResponse ( {
19
+ success : false ,
20
+ error :
21
+ "Not connected to a valid browser tools server. Please check your connection settings." ,
22
+ } ) ;
23
+ return ;
24
+ }
25
+
26
+ // Continue with screenshot capture
27
+ captureAndSendScreenshot ( message , settings , sendResponse ) ;
28
+ } )
29
+ . catch ( ( error ) => {
30
+ console . error ( "Error validating server:" , error ) ;
15
31
sendResponse ( {
16
32
success : false ,
17
- error : chrome . runtime . lastError . message ,
33
+ error : "Failed to validate server identity: " + error . message ,
18
34
} ) ;
19
- return ;
20
- }
35
+ } ) ;
36
+ } ) ;
37
+ return true ; // Required to use sendResponse asynchronously
38
+ }
39
+ } ) ;
40
+
41
+ // Validate server identity
42
+ async function validateServerIdentity ( host , port ) {
43
+ try {
44
+ const response = await fetch ( `http://${ host } :${ port } /.identity` , {
45
+ signal : AbortSignal . timeout ( 3000 ) , // 3 second timeout
46
+ } ) ;
47
+
48
+ if ( ! response . ok ) {
49
+ console . error ( `Invalid server response: ${ response . status } ` ) ;
50
+ return false ;
51
+ }
52
+
53
+ const identity = await response . json ( ) ;
54
+
55
+ // Validate the server signature
56
+ if ( identity . signature !== "mcp-browser-connector-24x7" ) {
57
+ console . error ( "Invalid server signature - not the browser tools server" ) ;
58
+ return false ;
59
+ }
21
60
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
- ) ;
61
+ return true ;
62
+ } catch ( error ) {
63
+ console . error ( "Error validating server identity:" , error ) ;
64
+ return false ;
65
+ }
66
+ }
67
+
68
+ // Function to capture and send screenshot
69
+ function captureAndSendScreenshot ( message , settings , sendResponse ) {
70
+ // Get the inspected window's tab
71
+ chrome . tabs . get ( message . tabId , ( tab ) => {
72
+ if ( chrome . runtime . lastError ) {
73
+ console . error ( "Error getting tab:" , chrome . runtime . lastError ) ;
74
+ sendResponse ( {
75
+ success : false ,
76
+ error : chrome . runtime . lastError . message ,
77
+ } ) ;
78
+ return ;
79
+ }
27
80
28
- if ( ! targetWindow ) {
29
- console . error ( "Could not find window containing the inspected tab" ) ;
81
+ // Get all windows to find the one containing our tab
82
+ chrome . windows . getAll ( { populate : true } , ( windows ) => {
83
+ const targetWindow = windows . find ( ( w ) =>
84
+ w . tabs . some ( ( t ) => t . id === message . tabId )
85
+ ) ;
86
+
87
+ if ( ! targetWindow ) {
88
+ console . error ( "Could not find window containing the inspected tab" ) ;
89
+ sendResponse ( {
90
+ success : false ,
91
+ error : "Could not find window containing the inspected tab" ,
92
+ } ) ;
93
+ return ;
94
+ }
95
+
96
+ // Capture screenshot of the window containing our tab
97
+ chrome . tabs . captureVisibleTab (
98
+ targetWindow . id ,
99
+ { format : "png" } ,
100
+ ( dataUrl ) => {
101
+ // Ignore DevTools panel capture error if it occurs
102
+ if (
103
+ chrome . runtime . lastError &&
104
+ ! chrome . runtime . lastError . message . includes ( "devtools://" )
105
+ ) {
106
+ console . error (
107
+ "Error capturing screenshot:" ,
108
+ chrome . runtime . lastError
109
+ ) ;
30
110
sendResponse ( {
31
111
success : false ,
32
- error : "Could not find window containing the inspected tab" ,
112
+ error : chrome . runtime . lastError . message ,
33
113
} ) ;
34
114
return ;
35
115
}
36
116
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
- ) ;
117
+ // Send screenshot data to browser connector using configured settings
118
+ const serverUrl = `http://${ settings . serverHost } :${ settings . serverPort } /screenshot` ;
119
+ console . log ( `Sending screenshot to ${ serverUrl } ` ) ;
120
+
121
+ fetch ( serverUrl , {
122
+ method : "POST" ,
123
+ headers : {
124
+ "Content-Type" : "application/json" ,
125
+ } ,
126
+ body : JSON . stringify ( {
127
+ data : dataUrl ,
128
+ path : message . screenshotPath ,
129
+ } ) ,
130
+ } )
131
+ . then ( ( response ) => response . json ( ) )
132
+ . then ( ( result ) => {
133
+ if ( result . error ) {
134
+ console . error ( "Error from server:" , result . error ) ;
135
+ sendResponse ( { success : false , error : result . error } ) ;
136
+ } else {
137
+ console . log ( "Screenshot saved successfully:" , result . path ) ;
138
+ // Send success response even if DevTools capture failed
51
139
sendResponse ( {
52
- success : false ,
53
- error : chrome . runtime . lastError . message ,
140
+ success : true ,
141
+ path : result . path ,
142
+ title : tab . title || "Current Tab" ,
54
143
} ) ;
55
- return ;
56
144
}
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
- ) ;
96
- } ) ;
97
- } ) ;
145
+ } )
146
+ . catch ( ( error ) => {
147
+ console . error ( "Error sending screenshot data:" , error ) ;
148
+ sendResponse ( {
149
+ success : false ,
150
+ error : error . message || "Failed to save screenshot" ,
151
+ } ) ;
152
+ } ) ;
153
+ }
154
+ ) ;
98
155
} ) ;
99
- return true ; // Required to use sendResponse asynchronously
100
- }
101
- } ) ;
156
+ } ) ;
157
+ }
0 commit comments