@@ -20,8 +20,30 @@ function isLocalhost(url) {
2020
2121// Helper function to find localhost tab
2222async 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+ }
2547}
2648
2749//keep alive functionality to address port disconnection issues
@@ -329,13 +351,22 @@ async function getActiveTab() {
329351 return localhostTab . id ;
330352 }
331353
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+
332363 // Fallback to current active tab
333364 const tabs = await chrome . tabs . query ( { active : true , currentWindow : true } ) ;
334365 if ( tabs . length > 0 ) {
335366 return tabs [ 0 ] . id ;
336367 }
337368
338- throw new Error ( 'No active tab' ) ;
369+ throw new Error ( errorMessage ) ;
339370 } catch ( error ) {
340371 console . error ( 'Error in getActiveTab:' , error ) ;
341372 throw error ;
0 commit comments