Skip to content

Commit edd1ab6

Browse files
committed
Refactor button injection to setup_observer and make it conditional based on prompt type
1 parent e28a41d commit edd1ab6

22 files changed

Lines changed: 66 additions & 56 deletions

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export const ai_studio: Chatbot = {
375375

376376
await close_panel()
377377
},
378-
inject_apply_response_button: (params) => {
378+
setup_observer: (params) => {
379379
const add_buttons = (footer: Element) => {
380380
add_apply_response_button({
381381
client_id: params.client_id,
@@ -424,6 +424,7 @@ export const ai_studio: Chatbot = {
424424
// AI Studio is quite sluggish with showing already generated tokens,
425425
// therefore we handle waiting for finished response differently than
426426
// in other chatbots.
427+
let has_sent_finished_responding = true
427428
let debounce_timer: NodeJS.Timeout
428429
const observer = new MutationObserver(() => {
429430
clearTimeout(debounce_timer)
@@ -438,16 +439,17 @@ export const ai_studio: Chatbot = {
438439
(span) => span.textContent?.trim() == 'thumb_up'
439440
)
440441
if (has_thumb_up) {
441-
const has_apply_button = !!footer.querySelector(
442-
'.cwc-apply-response-button'
443-
)
444-
if (!has_apply_button) {
442+
const has_been_processed = footer.hasAttribute('data-cwc-processed')
443+
if (!has_been_processed) {
445444
browser.runtime.sendMessage<Message>({
446445
action: 'finished-responding'
447446
})
448447
show_response_ready_notification({ chatbot_name: 'AI Studio' })
448+
footer.setAttribute('data-cwc-processed', 'true')
449+
}
450+
if (params.inject_button) {
451+
add_buttons(footer)
449452
}
450-
add_buttons(footer)
451453
}
452454
})
453455
}, 100)

apps/browser/src/content-scripts/send-prompt-content-script/chatbots/arena.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const arena: Chatbot = {
3232
input_element.value = params.message
3333
input_element.dispatchEvent(new Event('input', { bubbles: true }))
3434
},
35-
inject_apply_response_button: (params) => {
35+
setup_observer: (params) => {
3636
const add_buttons = (footer: Element) => {
3737
add_apply_response_button({
3838
client_id: params.client_id,
@@ -65,7 +65,7 @@ export const arena: Chatbot = {
6565
is_generating: () => !!document.querySelector('ol .animate-spin'),
6666
footer_selector:
6767
'ol .bg-surface-primary > div:last-child > div.text-text-primary',
68-
add_buttons
68+
add_buttons: params.inject_button ? add_buttons : undefined
6969
})
7070
}
7171
}

apps/browser/src/content-scripts/send-prompt-content-script/chatbots/chatgpt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export const chatgpt: Chatbot = {
130130
input_element.innerText = params.message
131131
input_element.dispatchEvent(new Event('input', { bubbles: true }))
132132
},
133-
inject_apply_response_button: (params) => {
133+
setup_observer: (params) => {
134134
const add_buttons = (footer: Element) => {
135135
add_apply_response_button({
136136
client_id: params.client_id,
@@ -161,7 +161,7 @@ export const chatgpt: Chatbot = {
161161
is_generating: () =>
162162
!!document.querySelector('button[data-testid="stop-button"]'),
163163
footer_selector: '.agent-turn > div:nth-of-type(2) > div',
164-
add_buttons
164+
add_buttons: params.inject_button ? add_buttons : undefined
165165
})
166166
}
167167
}

apps/browser/src/content-scripts/send-prompt-content-script/chatbots/claude.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export const claude: Chatbot = {
127127
input_element.dispatchEvent(new Event('input', { bubbles: true }))
128128
input_element.focus()
129129
},
130-
inject_apply_response_button: (params) => {
130+
setup_observer: (params) => {
131131
const add_buttons = (footer: Element) => {
132132
add_apply_response_button({
133133
client_id: params.client_id,
@@ -162,7 +162,7 @@ export const claude: Chatbot = {
162162
chatbot_name: 'Claude',
163163
is_generating: () => !!document.querySelector(stop_button_selector),
164164
footer_selector,
165-
add_buttons
165+
add_buttons: params.inject_button ? add_buttons : undefined
166166
})
167167
}
168168
}

apps/browser/src/content-scripts/send-prompt-content-script/chatbots/copilot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const copilot: Chatbot = {
3232
input_element.value = params.message
3333
input_element.dispatchEvent(new Event('input', { bubbles: true }))
3434
},
35-
inject_apply_response_button: (params) => {
35+
setup_observer: (params) => {
3636
const add_buttons = (footer: Element) => {
3737
add_apply_response_button({
3838
client_id: params.client_id,
@@ -62,7 +62,7 @@ export const copilot: Chatbot = {
6262
is_generating: () =>
6363
!!document.querySelector('button[data-testid="stop-button"]'),
6464
footer_selector: 'div[data-testid="message-item-reactions"]',
65-
add_buttons
65+
add_buttons: params.inject_button ? add_buttons : undefined
6666
})
6767
}
6868
}

apps/browser/src/content-scripts/send-prompt-content-script/chatbots/deepseek.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const deepseek: Chatbot = {
8080
input_element.value = params.message
8181
input_element.dispatchEvent(new Event('input', { bubbles: true }))
8282
},
83-
inject_apply_response_button: (params) => {
83+
setup_observer: (params) => {
8484
const add_buttons = (footer: Element) => {
8585
add_apply_response_button({
8686
client_id: params.client_id,
@@ -130,7 +130,7 @@ export const deepseek: Chatbot = {
130130
)
131131
},
132132
footer_selector: 'div.ds-message + div + div > div.ds-flex',
133-
add_buttons
133+
add_buttons: params.inject_button ? add_buttons : undefined
134134
})
135135
}
136136
}

apps/browser/src/content-scripts/send-prompt-content-script/chatbots/doubao.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const doubao: Chatbot = {
114114
input_element.value = params.message
115115
input_element.dispatchEvent(new Event('input', { bubbles: true }))
116116
},
117-
inject_apply_response_button: (params) => {
117+
setup_observer: (params) => {
118118
const add_buttons = (footer: Element) => {
119119
add_apply_response_button({
120120
client_id: params.client_id,
@@ -148,7 +148,7 @@ export const doubao: Chatbot = {
148148
?.classList.contains('!hidden'),
149149
footer_selector:
150150
'div[data-testid="message_action_bar"] > div > div > div',
151-
add_buttons
151+
add_buttons: params.inject_button ? add_buttons : undefined
152152
})
153153
}
154154
}

apps/browser/src/content-scripts/send-prompt-content-script/chatbots/gemini.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export const gemini: Chatbot = {
151151
input_element.dispatchEvent(new Event('input', { bubbles: true }))
152152
input_element.focus()
153153
},
154-
inject_apply_response_button: (params) => {
154+
setup_observer: (params) => {
155155
const add_buttons = (footer: Element) => {
156156
add_apply_response_button({
157157
client_id: params.client_id,
@@ -182,7 +182,7 @@ export const gemini: Chatbot = {
182182
is_generating: () =>
183183
!!document.querySelector('mat-icon[fonticon="stop"]'),
184184
footer_selector: 'message-actions > div > div',
185-
add_buttons
185+
add_buttons: params.inject_button ? add_buttons : undefined
186186
})
187187
}
188188
}

apps/browser/src/content-scripts/send-prompt-content-script/chatbots/github-copilot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const github_copilot: Chatbot = {
9696
input_element.dispatchEvent(new Event('input', { bubbles: true }))
9797
input_element.focus()
9898
},
99-
inject_apply_response_button: (params) => {
99+
setup_observer: (params) => {
100100
const add_buttons = (footer: Element) => {
101101
add_apply_response_button({
102102
client_id: params.client_id,
@@ -128,7 +128,7 @@ export const github_copilot: Chatbot = {
128128
'path[d="M5.75 4h4.5c.966 0 1.75.784 1.75 1.75v4.5A1.75 1.75 0 0 1 10.25 12h-4.5A1.75 1.75 0 0 1 4 10.25v-4.5C4 4.784 4.784 4 5.75 4Z"]'
129129
),
130130
footer_selector: 'div[data-testid="nonshared-toolbar"]',
131-
add_buttons
131+
add_buttons: params.inject_button ? add_buttons : undefined
132132
})
133133
}
134134
}

apps/browser/src/content-scripts/send-prompt-content-script/chatbots/grok.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export const grok: Chatbot = {
106106
input_element.dispatchEvent(new Event('input', { bubbles: true }))
107107
input_element.focus()
108108
},
109-
inject_apply_response_button: (params) => {
109+
setup_observer: (params) => {
110110
const add_buttons = (footer: Element) => {
111111
add_apply_response_button({
112112
client_id: params.client_id,
@@ -139,7 +139,7 @@ export const grok: Chatbot = {
139139
'path[d="M4 9.2v5.6c0 1.116 0 1.673.11 2.134a4 4 0 0 0 2.956 2.956c.46.11 1.018.11 2.134.11h5.6c1.116 0 1.673 0 2.134-.11a4 4 0 0 0 2.956-2.956c.11-.46.11-1.018.11-2.134V9.2c0-1.116 0-1.673-.11-2.134a4 4 0 0 0-2.956-2.955C16.474 4 15.916 4 14.8 4H9.2c-1.116 0-1.673 0-2.134.11a4 4 0 0 0-2.955 2.956C4 7.526 4 8.084 4 9.2Z"]'
140140
),
141141
footer_selector: 'div.items-start div.action-buttons > div',
142-
add_buttons
142+
add_buttons: params.inject_button ? add_buttons : undefined
143143
})
144144
}
145145
}

0 commit comments

Comments
 (0)