File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -341,6 +341,19 @@ async function openPanelForAllWindows() {
341341 return true ;
342342}
343343
344+ async function openPanelForWindow ( windowId ) {
345+ if ( ! chrome . sidePanel ?. open || typeof windowId !== "number" ) {
346+ return false ;
347+ }
348+
349+ try {
350+ await chrome . sidePanel . open ( { windowId } ) ;
351+ return true ;
352+ } catch {
353+ return false ;
354+ }
355+ }
356+
344357async function initializeSidePanelBehavior ( ) {
345358 if ( ! chrome . sidePanel ?. setPanelBehavior ) {
346359 return ;
@@ -383,9 +396,33 @@ chrome.runtime.onStartup.addListener(() => {
383396} ) ;
384397
385398chrome . action . onClicked . addListener ( ( ) => {
386- openPanelForAllWindows ( ) . catch ( ( error ) => {
387- console . error ( "Failed to open side panel" , error ) ;
388- } ) ;
399+ // Keep icon-click predictable: open current window panel first, then best-effort global open.
400+ chrome . windows
401+ . getLastFocused ( )
402+ . then ( async ( windowEntry ) => {
403+ const focusedWindowId = typeof windowEntry ?. id === "number" ? windowEntry . id : null ;
404+ let opened = false ;
405+
406+ if ( focusedWindowId !== null ) {
407+ opened = await openPanelForWindow ( focusedWindowId ) ;
408+ }
409+
410+ if ( ! opened ) {
411+ opened = await openPanelForAllWindows ( ) ;
412+ }
413+
414+ if ( ! opened ) {
415+ await chrome . tabs . create ( { url : getDashboardUrl ( ) } ) ;
416+ return ;
417+ }
418+
419+ openPanelForAllWindows ( ) . catch ( ( ) => {
420+ // Ignore follow-up global open failures.
421+ } ) ;
422+ } )
423+ . catch ( async ( ) => {
424+ await chrome . tabs . create ( { url : getDashboardUrl ( ) } ) ;
425+ } ) ;
389426} ) ;
390427
391428chrome . tabs . onCreated . addListener ( ( tab ) => {
You can’t perform that action at this time.
0 commit comments