@@ -20,8 +20,30 @@ function isLocalhost(url) {
20
20
21
21
// Helper function to find localhost tab
22
22
async function findLocalhostTab ( ) {
23
- const tabs = await chrome . tabs . query ( { currentWindow : true } ) ;
24
- return tabs . find ( ( tab ) => tab . url && isLocalhost ( tab . url ) ) ;
23
+ try {
24
+ // First check current window
25
+ const currentWindowTabs = await chrome . tabs . query ( { currentWindow : true } ) ;
26
+ const localhostTab = currentWindowTabs . find ( ( tab ) => tab . url && isLocalhost ( tab . url ) ) ;
27
+
28
+ if ( localhostTab ) {
29
+ return localhostTab ;
30
+ }
31
+
32
+ // If not found in current window, check all windows
33
+ const allTabs = await chrome . tabs . query ( { } ) ;
34
+ const localhostTabAnyWindow = allTabs . find ( ( tab ) => tab . url && isLocalhost ( tab . url ) ) ;
35
+
36
+ if ( localhostTabAnyWindow ) {
37
+ // Focus the window containing the localhost tab
38
+ await chrome . windows . update ( localhostTabAnyWindow . windowId , { focused : true } ) ;
39
+ return localhostTabAnyWindow ;
40
+ }
41
+
42
+ return null ;
43
+ } catch ( error ) {
44
+ console . error ( 'Error finding localhost tab:' , error ) ;
45
+ return null ;
46
+ }
25
47
}
26
48
27
49
//keep alive functionality to address port disconnection issues
@@ -329,13 +351,22 @@ async function getActiveTab() {
329
351
return localhostTab . id ;
330
352
}
331
353
354
+ // If no localhost tab is found, provide a more informative error
355
+ const errorMessage =
356
+ 'No localhost tab found. Please ensure:\n' +
357
+ '1. A React development server is running\n' +
358
+ '2. The server is using localhost\n' +
359
+ '3. The development page is open in Chrome' ;
360
+
361
+ console . warn ( errorMessage ) ;
362
+
332
363
// Fallback to current active tab
333
364
const tabs = await chrome . tabs . query ( { active : true , currentWindow : true } ) ;
334
365
if ( tabs . length > 0 ) {
335
366
return tabs [ 0 ] . id ;
336
367
}
337
368
338
- throw new Error ( 'No active tab' ) ;
369
+ throw new Error ( errorMessage ) ;
339
370
} catch ( error ) {
340
371
console . error ( 'Error in getActiveTab:' , error ) ;
341
372
throw error ;
0 commit comments