Skip to content

Commit 39bffac

Browse files
committed
Update MutationObserver to use document.documentElement instead of document.body
1 parent 17ef0cf commit 39bffac

4 files changed

Lines changed: 94 additions & 56 deletions

File tree

packages/browser/src/content-scripts/send-prompt-content-script/chatbots/ai-studio.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ export const ai_studio: Chatbot = {
446446
}, 100)
447447
})
448448

449-
observer.observe(document.body, {
449+
observer.observe(document.documentElement, {
450450
childList: true,
451451
subtree: true,
452452
characterData: true

packages/browser/src/content-scripts/send-prompt-content-script/send-prompt-content-script.ts

Lines changed: 82 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -184,64 +184,94 @@ const initialize_chat = async (params: { message: string; chat: Chat }) => {
184184
}
185185

186186
const 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

packages/browser/src/content-scripts/send-prompt-content-script/utils/add-apply-response-button.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function observe_for_responses(params: ResponseObserverParams) {
8484
})
8585
})
8686

87-
observer.observe(document.body, {
87+
observer.observe(document.documentElement, {
8888
childList: true,
8989
subtree: true,
9090
characterData: true

packages/vscode/src/views/panel/backend/message-handlers/handle-send-prompt.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,11 @@ async function show_preset_quick_pick(params: {
322322
const item = presets.find((p) => p.name == name)
323323

324324
if (item && !item.chatbot) {
325-
return { label: name, group_name: name, description: 'Group' }
325+
return {
326+
label: name.replace(/\s*\(\d+\)$/, ''),
327+
group_name: name,
328+
description: 'Group'
329+
}
326330
}
327331

328332
if (item && item.chatbot) {
@@ -334,7 +338,11 @@ async function show_preset_quick_pick(params: {
334338
? chatbot_models[preset.model]?.label || preset.model
335339
: ''
336340
return {
337-
label: `${is_unnamed ? preset.chatbot! : preset.name!}`,
341+
label: `${
342+
is_unnamed
343+
? preset.chatbot!
344+
: preset.name!.replace(/\s*\(\d+\)$/, '')
345+
}`,
338346
preset_name: preset.name,
339347
description: is_unnamed
340348
? model

0 commit comments

Comments
 (0)