@@ -184,64 +184,94 @@ const initialize_chat = async (params: { message: string; chat: Chat }) => {
184184}
185185
186186const main = async ( ) => {
187- if ( ! is_cwc_hash ) return
188-
189- // Remove the hash from the URL to avoid reloading the content script if the page is refreshed
190- history . replaceState (
191- null ,
192- '' ,
193- window . location . pathname + window . location . search
194- )
195-
196- // Get the message using the batch ID from the hash
197- const storage_key = `chat-init:${ batch_id } `
198- const storage = await browser . storage . local . get ( storage_key )
199- const stored_data = storage [ storage_key ] as {
200- text : string
201- current_chat : Chat
202- client_id : number
203- raw_instructions ?: string
204- edit_format ?: string
205- mode ?: 'ask' | 'edit-context' | 'code-completions' | 'no-context'
206- }
187+ const session_data_key = 'cwc-session-data'
188+
189+ if ( is_cwc_hash ) {
190+ // Remove the hash from the URL to avoid reloading the content script if the page is refreshed
191+ history . replaceState (
192+ null ,
193+ '' ,
194+ window . location . pathname + window . location . search
195+ )
207196
208- if ( ! stored_data ) {
209- console . error ( 'Chat initialization data not found for batch ID:' , batch_id )
210- return
211- }
197+ // Get the message using the batch ID from the hash
198+ const storage_key = `chat-init:${ batch_id } `
199+ const storage = await browser . storage . local . get ( storage_key )
200+ const stored_data = storage [ storage_key ] as {
201+ text : string
202+ current_chat : Chat
203+ client_id : number
204+ raw_instructions ?: string
205+ edit_format ?: string
206+ mode ?: 'ask' | 'edit-context' | 'code-completions' | 'no-context'
207+ }
212208
213- // Now directly use the current_chat instead of searching for it
214- const message_text = stored_data . text
215- const current_chat = stored_data . current_chat
209+ if ( ! stored_data ) {
210+ console . error (
211+ 'Chat initialization data not found for batch ID:' ,
212+ batch_id
213+ )
214+ return
215+ }
216216
217- if ( ! current_chat ) {
218- console . error ( 'Chat configuration not found' )
219- return
220- }
217+ // Now directly use the current_chat instead of searching for it
218+ const message_text = stored_data . text
219+ const current_chat = stored_data . current_chat
221220
222- if ( chatbot ?. wait_until_ready ) {
223- await chatbot . wait_until_ready ( )
224- }
221+ if ( ! current_chat ) {
222+ console . error ( 'Chat configuration not found' )
223+ return
224+ }
225225
226- await initialize_chat ( {
227- message : message_text ,
228- chat : current_chat
229- } )
226+ if ( chatbot ?. wait_until_ready ) {
227+ await chatbot . wait_until_ready ( )
228+ }
230229
231- // Clean up the storage entry after using it
232- await browser . storage . local . remove ( storage_key )
233-
234- if (
235- chatbot ?. inject_apply_response_button &&
236- ( ! stored_data . mode ||
237- stored_data . mode == 'edit-context' ||
238- stored_data . mode == 'code-completions' )
239- ) {
240- chatbot . inject_apply_response_button (
241- stored_data . client_id ,
242- stored_data . raw_instructions ,
243- stored_data . edit_format
244- )
230+ await initialize_chat ( {
231+ message : message_text ,
232+ chat : current_chat
233+ } )
234+
235+ // Clean up the storage entry after using it
236+ await browser . storage . local . remove ( storage_key )
237+
238+ if (
239+ chatbot ?. inject_apply_response_button &&
240+ ( stored_data . mode == 'edit-context' ||
241+ stored_data . mode == 'code-completions' )
242+ ) {
243+ sessionStorage . setItem (
244+ session_data_key ,
245+ JSON . stringify ( {
246+ client_id : stored_data . client_id ,
247+ raw_instructions : stored_data . raw_instructions ,
248+ edit_format : stored_data . edit_format
249+ } )
250+ )
251+ chatbot . inject_apply_response_button (
252+ stored_data . client_id ,
253+ stored_data . raw_instructions ,
254+ stored_data . edit_format
255+ )
256+ } else {
257+ sessionStorage . removeItem ( session_data_key )
258+ }
259+ } else {
260+ const session_data_str = sessionStorage . getItem ( session_data_key )
261+ if ( session_data_str && chatbot ) {
262+ try {
263+ const session_data = JSON . parse ( session_data_str )
264+ if ( chatbot . inject_apply_response_button ) {
265+ chatbot . inject_apply_response_button (
266+ session_data . client_id ,
267+ session_data . raw_instructions ,
268+ session_data . edit_format
269+ )
270+ }
271+ } catch ( e ) {
272+ console . error ( 'Failed to parse CWC session data' , e )
273+ }
274+ }
245275 }
246276}
247277
0 commit comments