@@ -64,6 +64,42 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
6464 sendResponse ( { success : true } ) ;
6565 }
6666
67+ if ( message . type === 'OPEN_SIDE_PANEL_FOR_ANNOTATION' ) {
68+ // Open sidepanel in response to annotation click (user gesture preserved)
69+ // Must call sidePanel.open() immediately to preserve user gesture context
70+ const tabId = sender . tab ?. id ;
71+ if ( tabId ) {
72+ chrome . sidePanel . open ( { tabId } , ( ) => {
73+ if ( chrome . runtime . lastError ) {
74+ logger . error ( 'Background' , 'Failed to open sidepanel' , new Error ( chrome . runtime . lastError . message ) ) ;
75+ sendResponse ( { success : false , error : chrome . runtime . lastError . message } ) ;
76+ } else {
77+ logger . info ( 'Background' , 'Sidepanel opened for annotation' , {
78+ annotationId : message . annotationId ,
79+ tabId
80+ } ) ;
81+
82+ // Send scroll message after a short delay to allow sidepanel to load
83+ setTimeout ( ( ) => {
84+ chrome . runtime . sendMessage ( {
85+ type : 'SCROLL_TO_ANNOTATION' ,
86+ annotationId : message . annotationId ,
87+ } ) . catch ( ( ) => {
88+ // Sidepanel might not be ready yet, that's okay
89+ logger . debug ( 'Background' , 'Sidepanel not ready for scroll message yet' ) ;
90+ } ) ;
91+ } , 300 ) ;
92+
93+ sendResponse ( { success : true } ) ;
94+ }
95+ } ) ;
96+ } else {
97+ logger . warn ( 'Background' , 'No tab ID available for opening sidepanel' ) ;
98+ sendResponse ( { success : false , error : 'No tab ID available' } ) ;
99+ }
100+ return true ; // Keep message channel open for async response
101+ }
102+
67103 if ( message . type === MESSAGE_TYPES . CREATE_ANNOTATION ) {
68104 // Handle annotation creation
69105 handleCreateAnnotation ( message . annotation )
@@ -643,54 +679,47 @@ chrome.webNavigation.onBeforeNavigate.addListener((details) => {
643679
644680// Handle keyboard commands
645681// NOTE: Must NOT use async/await here to preserve user gesture context
646- console . log ( '[Graphiti] Registering keyboard command listener') ;
682+ logger . info ( 'Background' , ' Registering keyboard command listener') ;
647683chrome . commands . onCommand . addListener ( ( command ) => {
648- // Use console.log directly for immediate visibility in service worker console
649- console . log ( '[Graphiti] Command received:' , command ) ;
650684 logger . info ( 'Background' , 'Command received' , { command } ) ;
651685
652686 if ( command === COMMAND_NAMES . TOGGLE_SIDEPANEL ) {
653- console . log ( '[Graphiti] toggle-sidepanel command triggered') ;
687+ logger . info ( 'Background' , ' toggle-sidepanel command triggered') ;
654688 // Open the side panel - must call open() immediately to preserve user gesture
655689 // Using windowId instead of tabId since it's available synchronously
656690 chrome . windows . getCurrent ( ( window ) => {
657691 if ( ! window ?. id ) {
658- console . warn ( '[Graphiti] No current window found' ) ;
659692 logger . warn ( 'Background' , 'No current window found for side panel toggle' ) ;
660693 return ;
661694 }
662695
663- console . log ( '[Graphiti] Opening side panel for window:' , window . id ) ;
696+ logger . info ( 'Background' , ' Opening side panel' , { windowId : window . id } ) ;
664697 chrome . sidePanel . open ( { windowId : window . id } , ( ) => {
665698 if ( chrome . runtime . lastError ) {
666- console . error ( '[Graphiti] Failed to open:' , chrome . runtime . lastError . message ) ;
667699 logger . error ( 'Background' , 'Failed to open side panel' , new Error ( chrome . runtime . lastError . message ) ) ;
668700 } else {
669- console . log ( '[Graphiti] Side panel opened successfully' ) ;
670701 logger . info ( 'Background' , 'Side panel opened via keyboard shortcut' , { windowId : window . id } ) ;
671702 }
672703 } ) ;
673704 } ) ;
674705 }
675706
676707 if ( command === COMMAND_NAMES . OPEN_ANNOTATIONS ) {
677- console . log ( '[Graphiti] open-annotations command triggered') ;
708+ logger . info ( 'Background' , ' open-annotations command triggered') ;
678709 // Open side panel and switch to annotations tab
679710 // Must call open() immediately to preserve user gesture
680711 chrome . windows . getCurrent ( ( window ) => {
681712 if ( ! window ?. id ) {
682- console . warn ( '[Graphiti] No current window found' ) ;
683713 logger . warn ( 'Background' , 'No current window found for annotations' ) ;
684714 return ;
685715 }
686716
687- console . log ( '[Graphiti] Opening side panel for annotations, window:' , window . id ) ;
717+ logger . info ( 'Background' , ' Opening side panel for annotations' , { windowId : window . id } ) ;
688718 chrome . sidePanel . open ( { windowId : window . id } , ( ) => {
689719 if ( chrome . runtime . lastError ) {
690- console . error ( '[Graphiti] Failed to open:' , chrome . runtime . lastError . message ) ;
691720 logger . error ( 'Background' , 'Failed to open annotations' , new Error ( chrome . runtime . lastError . message ) ) ;
692721 } else {
693- console . log ( '[Graphiti] Side panel opened, switching to annotations tab... ') ;
722+ logger . info ( 'Background' , ' Side panel opened, switching to annotations tab') ;
694723 // Send message to sidebar to switch to annotations tab
695724 setTimeout ( ( ) => {
696725 chrome . runtime . sendMessage ( {
@@ -709,19 +738,18 @@ chrome.commands.onCommand.addListener((command) => {
709738 }
710739
711740 if ( command === COMMAND_NAMES . TOGGLE_DRAWING ) {
712- console . log ( '[Graphiti] toggle-drawing command triggered') ;
741+ logger . info ( 'Background' , ' toggle-drawing command triggered') ;
713742 // Toggle drawing mode on the current tab
714743 chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
715744 const tab = tabs [ 0 ] ;
716- console . log ( '[Graphiti] Active tab for drawing: ', tab ?. id , tab ?. url ) ;
745+ logger . info ( 'Background' , ' Active tab for drawing', { tabId : tab ?. id , url : tab ?. url } ) ;
717746
718747 if ( tab ?. id && tab . url && ! tab . url . startsWith ( 'chrome://' ) && ! tab . url . startsWith ( 'about:' ) && ! tab . url . startsWith ( 'chrome-extension://' ) ) {
719- console . log ( '[Graphiti] Sending TOGGLE_DRAWING_MODE to tab', tab . id ) ;
748+ logger . info ( 'Background' , ' Sending TOGGLE_DRAWING_MODE to tab', { tabId : tab . id } ) ;
720749 chrome . tabs . sendMessage ( tab . id , {
721750 type : 'TOGGLE_DRAWING_MODE' ,
722751 } , ( response ) => {
723752 if ( chrome . runtime . lastError ) {
724- console . error ( '[Graphiti] Drawing mode error:' , chrome . runtime . lastError . message ) ;
725753 logger . error ( 'Background' , 'Failed to toggle drawing mode - content script may not be ready' , new Error ( chrome . runtime . lastError . message ) ) ;
726754 // Try to notify user
727755 chrome . notifications ?. create ( {
@@ -731,15 +759,13 @@ chrome.commands.onCommand.addListener((command) => {
731759 message : 'Please refresh the page to use drawing mode on this site.'
732760 } ) ;
733761 } else {
734- console . log ( '[Graphiti] Drawing mode toggled successfully:' , response ?. active ) ;
735762 logger . info ( 'Background' , 'Drawing mode toggled via keyboard shortcut' , {
736763 tabId : tab . id ,
737764 active : response ?. active
738765 } ) ;
739766 }
740767 } ) ;
741768 } else {
742- console . warn ( '[Graphiti] Cannot use drawing mode on this page:' , tab ?. url ) ;
743769 logger . warn ( 'Background' , 'Cannot use drawing mode on this page' , { url : tab ?. url } ) ;
744770 chrome . notifications ?. create ( {
745771 type : 'basic' ,
@@ -752,7 +778,7 @@ chrome.commands.onCommand.addListener((command) => {
752778 }
753779} ) ;
754780
755- console . log ( '[Graphiti] Background script command listeners registered') ;
781+ logger . info ( ' Background' , 'Command listeners registered') ;
756782
757783// Handle errors
758784self . addEventListener ( 'error' , ( event ) => {
0 commit comments