@@ -115,6 +115,45 @@ document.addEventListener('DOMContentLoaded', () => {
115115 if ( showRatingIcon ) showRatingIcon . textContent = result . showRating ? '✅' : '❌' ;
116116 } ) ;
117117
118+ // Helper function to send messages safely to content script
119+ function safelySendMessage ( message : any ) {
120+ chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
121+ const tabId = tabs [ 0 ] ?. id ;
122+ if ( ! tabs || ! tabs [ 0 ] || typeof tabId === 'undefined' ) {
123+ return ;
124+ }
125+
126+ try {
127+ // Send message to background script for settings updates
128+ if ( message . action === 'updateDescription' ) {
129+ chrome . runtime . sendMessage ( { action : 'settingsUpdate' } ) ;
130+ return ;
131+ }
132+
133+ // For other messages, send directly to content script
134+ chrome . tabs . sendMessage ( tabId , message , response => {
135+ if ( chrome . runtime . lastError ) {
136+ console . error ( 'Error sending message:' , chrome . runtime . lastError . message ) ;
137+ // Attempt to inject the content script if it's not already injected
138+ chrome . scripting . executeScript ( {
139+ target : { tabId } ,
140+ files : [ 'dist/content-script/update-description-tab.js' ]
141+ } ) . then ( ( ) => {
142+ // Try sending the message again after injecting the script
143+ setTimeout ( ( ) => {
144+ chrome . tabs . sendMessage ( tabId , message ) ;
145+ } , 100 ) ;
146+ } ) . catch ( err => {
147+ console . error ( 'Error injecting content script:' , err ) ;
148+ } ) ;
149+ }
150+ } ) ;
151+ } catch ( error ) {
152+ console . error ( 'Error sending message:' , error ) ;
153+ }
154+ } ) ;
155+ }
156+
118157 // Set up toggle event handlers
119158 const showCompanyTagsBtn = document . getElementById ( 'show-company-tags-btn' ) ;
120159 showCompanyTagsBtn && showCompanyTagsBtn . addEventListener ( 'click' , function ( ) {
@@ -123,9 +162,7 @@ document.addEventListener('DOMContentLoaded', () => {
123162 chrome . storage . local . set ( { showCompanyTags : showCompanyTags } , ( ) => {
124163 const showCompanyTagsIcon = document . getElementById ( 'show-company-tags-icon' ) ;
125164 showCompanyTagsIcon && ( showCompanyTagsIcon . textContent = showCompanyTags ? '✅' : '❌' ) ;
126- chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
127- chrome . tabs . sendMessage ( tabs [ 0 ] . id || 0 , { action : 'updateDescription' , title : tabs [ 0 ] . title || 'title' } ) ;
128- } ) ;
165+ safelySendMessage ( { action : 'updateDescription' , title : document . title || 'title' } ) ;
129166 } ) ;
130167 } ) ;
131168 } ) ;
@@ -137,10 +174,7 @@ document.addEventListener('DOMContentLoaded', () => {
137174 chrome . storage . local . set ( { showExamples : showExamples } , ( ) => {
138175 const showExamplesIcon = document . getElementById ( 'show-examples-icon' ) ;
139176 showExamplesIcon && ( showExamplesIcon . textContent = showExamples ? '✅' : '❌' ) ;
140- } ) ;
141- // Manually trigger the update description after toggling
142- chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
143- chrome . tabs . sendMessage ( tabs [ 0 ] . id || 0 , { action : 'updateDescription' , title : tabs [ 0 ] . title || 'title' } ) ;
177+ safelySendMessage ( { action : 'updateDescription' , title : document . title || 'title' } ) ;
144178 } ) ;
145179 } ) ;
146180 } ) ;
@@ -152,10 +186,7 @@ document.addEventListener('DOMContentLoaded', () => {
152186 chrome . storage . local . set ( { showDifficulty : showDifficulty } , ( ) => {
153187 const showDifficultyIcon = document . getElementById ( 'show-difficulty-icon' ) ;
154188 if ( showDifficultyIcon ) showDifficultyIcon . textContent = showDifficulty ? '✅' : '❌' ;
155- } ) ;
156- // Manually trigger the update description after toggling
157- chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
158- chrome . tabs . sendMessage ( tabs [ 0 ] . id || 0 , { action : 'updateDescription' , title : tabs [ 0 ] . title || 'title' } ) ;
189+ safelySendMessage ( { action : 'updateDescription' , title : document . title || 'title' } ) ;
159190 } ) ;
160191 } ) ;
161192 } ) ;
@@ -167,10 +198,7 @@ document.addEventListener('DOMContentLoaded', () => {
167198 chrome . storage . local . set ( { showRating : showRating } , ( ) => {
168199 const showRatingIcon = document . getElementById ( 'show-rating-icon' ) ;
169200 if ( showRatingIcon ) showRatingIcon . textContent = showRating ? '✅' : '❌' ;
170- } ) ;
171- // Manually trigger the update description after toggling
172- chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
173- chrome . tabs . sendMessage ( tabs [ 0 ] . id || 0 , { action : 'updateDescription' , title : tabs [ 0 ] . title || 'title' } ) ;
201+ safelySendMessage ( { action : 'updateDescription' , title : document . title || 'title' } ) ;
174202 } ) ;
175203 } ) ;
176204 } ) ;
0 commit comments