Skip to content

Commit 6b4536f

Browse files
committed
Retrieve session data for apply response actions and prevent duplicate mutation observers
- when re-using the same browser tab, 'raw instructions' are sent back of the message that opened the tab, not the latest, subsequent one
1 parent 6dcbe56 commit 6b4536f

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,30 @@ export function add_apply_response_button(params: {
4848
requestAnimationFrame(async () => {
4949
await params.perform_copy(params.footer)
5050
await new Promise((resolve) => setTimeout(resolve, 500))
51+
52+
let client_id = params.client_id
53+
let raw_instructions = params.raw_instructions
54+
let edit_format = params.edit_format
55+
56+
try {
57+
const session_data_str = sessionStorage.getItem('cwc-session-data')
58+
if (session_data_str) {
59+
const session_data = JSON.parse(session_data_str)
60+
if (session_data.client_id !== undefined) {
61+
client_id = session_data.client_id
62+
raw_instructions = session_data.raw_instructions
63+
edit_format = session_data.edit_format
64+
}
65+
}
66+
} catch (error) {
67+
console.error('Error reading CWC session data', error)
68+
}
69+
5170
browser.runtime.sendMessage<Message>({
5271
action: 'apply-response',
53-
client_id: params.client_id,
54-
raw_instructions: params.raw_instructions,
55-
edit_format: params.edit_format,
72+
client_id,
73+
raw_instructions,
74+
edit_format,
5675
url: window.location.href
5776
})
5877
})
@@ -62,12 +81,18 @@ export function add_apply_response_button(params: {
6281
apply_response_button.focus({ preventScroll: true })
6382
}
6483

84+
let active_observer: MutationObserver | null = null
85+
6586
export function observe_for_responses(params: {
6687
chatbot_name: string
6788
is_generating: () => boolean
6889
footer_selector: string
6990
add_buttons?: (footer: Element) => void
7091
}) {
92+
if (active_observer) {
93+
active_observer.disconnect()
94+
}
95+
7196
let has_sent_finished_responding = true
7297

7398
const observer = new MutationObserver(() => {
@@ -98,6 +123,8 @@ export function observe_for_responses(params: {
98123
})
99124
})
100125

126+
active_observer = observer
127+
101128
observer.observe(document.documentElement, {
102129
childList: true,
103130
subtree: true,

0 commit comments

Comments
 (0)