@@ -115,6 +115,45 @@ document.addEventListener('DOMContentLoaded', () => {
115
115
if ( showRatingIcon ) showRatingIcon . textContent = result . showRating ? '✅' : '❌' ;
116
116
} ) ;
117
117
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
+
118
157
// Set up toggle event handlers
119
158
const showCompanyTagsBtn = document . getElementById ( 'show-company-tags-btn' ) ;
120
159
showCompanyTagsBtn && showCompanyTagsBtn . addEventListener ( 'click' , function ( ) {
@@ -123,9 +162,7 @@ document.addEventListener('DOMContentLoaded', () => {
123
162
chrome . storage . local . set ( { showCompanyTags : showCompanyTags } , ( ) => {
124
163
const showCompanyTagsIcon = document . getElementById ( 'show-company-tags-icon' ) ;
125
164
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' } ) ;
129
166
} ) ;
130
167
} ) ;
131
168
} ) ;
@@ -137,10 +174,7 @@ document.addEventListener('DOMContentLoaded', () => {
137
174
chrome . storage . local . set ( { showExamples : showExamples } , ( ) => {
138
175
const showExamplesIcon = document . getElementById ( 'show-examples-icon' ) ;
139
176
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' } ) ;
144
178
} ) ;
145
179
} ) ;
146
180
} ) ;
@@ -152,10 +186,7 @@ document.addEventListener('DOMContentLoaded', () => {
152
186
chrome . storage . local . set ( { showDifficulty : showDifficulty } , ( ) => {
153
187
const showDifficultyIcon = document . getElementById ( 'show-difficulty-icon' ) ;
154
188
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' } ) ;
159
190
} ) ;
160
191
} ) ;
161
192
} ) ;
@@ -167,10 +198,7 @@ document.addEventListener('DOMContentLoaded', () => {
167
198
chrome . storage . local . set ( { showRating : showRating } , ( ) => {
168
199
const showRatingIcon = document . getElementById ( 'show-rating-icon' ) ;
169
200
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' } ) ;
174
202
} ) ;
175
203
} ) ;
176
204
} ) ;
0 commit comments