@@ -63,7 +63,6 @@ const process_next_chat = async () => {
6363
6464 const current_queue_item = chat_queue [ 0 ]
6565
66- // Clear any existing timeout
6766 if ( current_queue_item . timeout_id ) {
6867 clearTimeout ( current_queue_item . timeout_id )
6968 current_queue_item . timeout_id = undefined
@@ -72,11 +71,9 @@ const process_next_chat = async () => {
7271 if (
7372 current_queue_item . current_index >= current_queue_item . message . chats . length
7473 ) {
75- // Current queue item is complete, remove it and process the next one
7674 chat_queue . shift ( )
7775
7876 if ( chat_queue . length > 0 ) {
79- // Start processing the next queue item
8077 chat_queue [ 0 ] . current_index = 0
8178 await process_next_chat ( )
8279 } else {
@@ -88,10 +85,8 @@ const process_next_chat = async () => {
8885 const current_chat =
8986 current_queue_item . message . chats [ current_queue_item . current_index ]
9087
91- // Generate a unique 3-character alphanumeric batch ID
9288 const batch_id = await generate_alphanumeric_id ( 'chat-init' )
9389
94- // Store only the relevant information - the text and the current chat configuration
9590 await browser . storage . local . set ( {
9691 [ `chat-init:${ batch_id } ` ] : {
9792 text : current_queue_item . message . text ,
@@ -118,14 +113,12 @@ const process_next_chat = async () => {
118113 active : true
119114 } )
120115 } else {
121- // Open the tab with the current chat URL
122116 browser . tabs . create ( {
123117 url : `${ current_chat . url } #cwc-${ batch_id } ` ,
124118 active : true
125119 } )
126120 }
127121
128- // Increment the current index for the next chat
129122 current_queue_item . current_index ++
130123
131124 // Set a timeout to automatically proceed if no confirmation is received
@@ -150,14 +143,12 @@ const handle_initialize_chats_message = async (
150143 message : InitializeChatsMessage
151144) => {
152145 if ( message . chats && message . chats . length > 0 ) {
153- // Add the new request to the queue
154146 chat_queue . push ( {
155147 message,
156148 remaining_chats : message . chats . length ,
157149 current_index : 0
158150 } )
159151
160- // Start processing if not already doing so
161152 await start_processing ( )
162153 }
163154}
@@ -191,9 +182,7 @@ const handle_initialize_chat_message = async (
191182}
192183
193184const handle_chat_initialized = async ( ) => {
194- // Process the next chat in the queue if one exists
195185 if ( chat_queue . length > 0 ) {
196- // Clear the timeout since we received confirmation
197186 if ( chat_queue [ 0 ] . timeout_id ) {
198187 clearTimeout ( chat_queue [ 0 ] . timeout_id )
199188 chat_queue [ 0 ] . timeout_id = undefined
@@ -209,19 +198,37 @@ const handle_get_tab_data = async (
209198) => {
210199 try {
211200 const tabs = await browser . tabs . query ( { active : true , currentWindow : true } )
212- const url = tabs [ 0 ] ?. url
201+ const tab = tabs [ 0 ]
213202 const favicon_url = tabs [ 0 ] ?. favIconUrl
214203
215- if ( ! url || ! url . startsWith ( 'http' ) ) {
204+ if ( ! tab ?. url || ! tab . url . startsWith ( 'http' ) ) {
216205 throw new Error ( 'URL is not valid' )
217206 }
218207
219208 let html = ''
220- const response = await fetch ( url )
221- if ( ! response . ok ) {
222- throw new Error ( `HTTP error! Status: ${ response . status } ` )
209+
210+ // (Firefox uses browserAction, Chrome uses action)
211+ const is_firefox = typeof browser . browserAction !== 'undefined'
212+
213+ if ( is_firefox && tab . id ) {
214+ // Firefox: Use the older tabs.executeScript API (MV2 style)
215+ const results = await browser . tabs . executeScript ( tab . id , {
216+ code : 'document.documentElement.outerHTML'
217+ } )
218+
219+ if ( ! results || results . length === 0 || ! results [ 0 ] ) {
220+ throw new Error ( 'Failed to get page content' )
221+ }
222+
223+ html = results [ 0 ] as string
224+ } else {
225+ // Chrome: Use fetch
226+ const response = await fetch ( tab . url )
227+ if ( ! response . ok ) {
228+ throw new Error ( `HTTP error! Status: ${ response . status } ` )
229+ }
230+ html = await response . text ( )
223231 }
224- html = await response . text ( )
225232
226233 let favicon_base64 : string | undefined
227234 if ( favicon_url ) {
@@ -264,7 +271,7 @@ export const setup_message_listeners = () => {
264271 return true
265272 }
266273 }
267- return false // For messages that don't need a response
274+ return false
268275 }
269276 )
270277}
0 commit comments