Skip to content

Commit a2ea9dc

Browse files
committed
Replace injected apply buttons with native copy button binding
- instead of injecting 'Apply' button in response's body - listen for 'copy' button click directly. Also focus on them when scrolling down. - should not scroll down in ai studio - on some chatbots copy button is not outlined, but manually using TAB we can outline it (e.g. claude) - `apps/browser/src/content-scripts/send-prompt-content-script/chatbots/yuanbao.ts`'s observer setup is not implemented in standard way └── apps └── browser ├── .gitignore ├── README.md ├── package.json ├── scripts │ └── build.mjs ├── src │ ├── background │ │ ├── clear-chat-init-data.ts │ │ ├── config.ts │ │ ├── keep-alive.ts │ │ ├── main.ts │ │ ├── message-handler.ts │ │ └── websocket.ts │ ├── content-scripts │ │ └── send-prompt-content-script │ │ ├── chatbots │ │ │ ├── ai-studio.ts │ │ │ ├── arena.ts │ │ │ ├── chatgpt.ts │ │ │ ├── claude.ts │ │ │ ├── copilot.ts │ │ │ ├── deepseek.ts │ │ │ ├── doubao.ts │ │ │ ├── gemini.ts │ │ │ ├── github-copilot.ts │ │ │ ├── grok.ts │ │ │ ├── hugging-chat.ts │ │ │ ├── index.ts │ │ │ ├── kimi.ts │ │ │ ├── meta.ts │ │ │ ├── mistral.ts │ │ │ ├── open-webui.ts │ │ │ ├── openrouter.ts │ │ │ ├── qwen.ts │ │ │ ├── together.ts │ │ │ ├── yuanbao.ts │ │ │ └── z-ai.ts │ │ ├── send-prompt-content-script.ts │ │ ├── types │ │ │ └── chatbot.ts │ │ └── utils │ │ ├── bind-copy-button.ts │ │ ├── report-initialization-error.ts │ │ ├── session-state.ts │ │ └── show-response-ready-notification.ts │ ├── fonts │ │ └── Bangers-Regular.ttf │ ├── icons │ │ ├── icon-128.png │ │ ├── icon-16.png │ │ ├── icon-32.png │ │ └── icon-48.png │ ├── manifest.json │ ├── types │ │ └── messages.ts │ └── utils │ └── is-message.ts ├── tsconfig.json └── vite.config.ts
1 parent 196e574 commit a2ea9dc

28 files changed

Lines changed: 329 additions & 514 deletions

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

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Message } from '@/types/messages'
22
import { Chatbot } from '../types/chatbot'
33
import { CHATBOTS } from '@shared/constants/chatbots'
44
import { show_response_ready_notification } from '../utils/show-response-ready-notification'
5-
import { add_apply_response_button } from '../utils/add-apply-response-button'
5+
import { trigger_apply } from '../utils/bind-copy-button'
66
import { report_initialization_error } from '../utils/report-initialization-error'
77

88
export const ai_studio: Chatbot = {
@@ -218,46 +218,13 @@ export const ai_studio: Chatbot = {
218218
await close_panel()
219219
},
220220
setup_observer: (params) => {
221-
const add_buttons = (footer: Element) => {
222-
add_apply_response_button({
223-
client_id: params.client_id,
224-
raw_instructions: params.raw_instructions,
225-
footer,
226-
perform_copy: (f) => {
227-
const chat_turn_container = f.closest('.chat-turn-container')
228-
if (!chat_turn_container) {
229-
report_initialization_error({
230-
function_name: 'ai_studio.perform_copy',
231-
log_message: 'Chat turn container not found'
232-
})
233-
return
234-
}
235-
const options = chat_turn_container.querySelector(
236-
'ms-chat-turn-options > div > button'
237-
) as HTMLElement
238-
if (!options) {
239-
report_initialization_error({
240-
function_name: 'ai_studio.perform_copy',
241-
log_message: 'Options button not found'
242-
})
243-
return
244-
}
245-
options.click()
246-
const markdown_copy_button = Array.from(
247-
document.querySelectorAll('button')
248-
).find((button) =>
249-
button.textContent?.includes('markdown_copy')
250-
) as HTMLElement
251-
if (!markdown_copy_button) {
252-
report_initialization_error({
253-
function_name: 'ai_studio.perform_copy',
254-
log_message: 'Markdown copy button not found'
255-
})
256-
return
257-
}
258-
markdown_copy_button.click()
259-
},
260-
insert_button: (f, b) => f.insertBefore(b, f.children[2])
221+
if (params.bind_copy) {
222+
document.addEventListener('click', (e) => {
223+
const target = e.target as HTMLElement
224+
const button = target.closest('button')
225+
if (button && button.textContent?.includes('markdown_copy')) {
226+
trigger_apply(params)
227+
}
261228
})
262229
}
263230

@@ -286,9 +253,6 @@ export const ai_studio: Chatbot = {
286253
show_response_ready_notification({ chatbot_name: 'AI Studio' })
287254
footer.setAttribute('data-cwc-processed', 'true')
288255
}
289-
if (params.inject_button) {
290-
add_buttons(footer)
291-
}
292256
}
293257
})
294258
}, 100)

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Chatbot } from '../types/chatbot'
22
import {
3-
add_apply_response_button,
3+
bind_copy_button,
44
observe_for_responses
5-
} from '../utils/add-apply-response-button'
5+
} from '../utils/bind-copy-button'
66
import { report_initialization_error } from '../utils/report-initialization-error'
77

88
export const arena: Chatbot = {
@@ -33,25 +33,24 @@ export const arena: Chatbot = {
3333
input_element.dispatchEvent(new Event('input', { bubbles: true }))
3434
},
3535
setup_observer: (params) => {
36-
const add_buttons = (footer: Element) => {
37-
add_apply_response_button({
36+
const bind_buttons = (footer: Element) => {
37+
bind_copy_button({
3838
client_id: params.client_id,
3939
raw_instructions: params.raw_instructions,
4040
footer,
41-
perform_copy: (f) => {
41+
get_copy_button: (f) => {
4242
const copy_button = f.querySelector(
4343
'button:has([d="M15 9V4.6C15 4.26863 14.7314 4 14.4 4H4.6C4.26863 4 4 4.26863 4 4.6V14.4C4 14.7314 4.26863 15 4.6 15H9"])'
4444
) as HTMLElement
4545
if (!copy_button) {
4646
report_initialization_error({
47-
function_name: 'arena.perform_copy',
47+
function_name: 'arena.get_copy_button',
4848
log_message: 'Copy button not found'
4949
})
50-
return
50+
return null
5151
}
52-
copy_button.click()
53-
},
54-
insert_button: (f, b) => f.insertBefore(b, f.children[0])
52+
return copy_button
53+
}
5554
})
5655
}
5756

@@ -60,7 +59,7 @@ export const arena: Chatbot = {
6059
is_generating: () => !!document.querySelector('ol .animate-spin'),
6160
footer_selector:
6261
'ol .bg-surface-primary > div:last-child > div > div, [role="presentation"] > div',
63-
add_buttons: params.inject_button ? add_buttons : undefined
62+
bind_buttons: params.bind_copy ? bind_buttons : undefined
6463
})
6564
}
6665
}

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { CHATBOTS } from '@shared/constants/chatbots'
22
import { Chatbot } from '../types/chatbot'
33
import {
4-
add_apply_response_button,
4+
bind_copy_button,
55
observe_for_responses
6-
} from '../utils/add-apply-response-button'
6+
} from '../utils/bind-copy-button'
77
import { report_initialization_error } from '../utils/report-initialization-error'
88

99
export const chatgpt: Chatbot = {
@@ -14,16 +14,8 @@ export const chatgpt: Chatbot = {
1414
'div[contenteditable="true"]'
1515
) as HTMLElement
1616

17-
// const thread_header_button = document.querySelector(
18-
// 'div[data-testid="thread-header-right-actions"] > div:first-child button'
19-
// )
20-
21-
// const reasoning_effort_button = document.querySelector(
22-
// 'div[data-composer-transition-slot="trailing"] > div:first-child button'
23-
// )
24-
2517
const is_ready = true
26-
// thread_header_button !== null || reasoning_effort_button !== null
18+
// thread_heder_button !== null || reasoning_effort_button !== null
2719

2820
if (input_element && is_ready) {
2921
input_element.innerText = ' '
@@ -180,26 +172,24 @@ export const chatgpt: Chatbot = {
180172
input_element.dispatchEvent(new Event('input', { bubbles: true }))
181173
},
182174
setup_observer: (params) => {
183-
const add_buttons = (footer: Element) => {
184-
add_apply_response_button({
175+
const bind_buttons = (footer: Element) => {
176+
bind_copy_button({
185177
client_id: params.client_id,
186178
raw_instructions: params.raw_instructions,
187179
footer,
188-
perform_copy: (f) => {
180+
get_copy_button: (f) => {
189181
const copy_button = f.querySelector(
190182
'button:first-child'
191183
) as HTMLElement
192184
if (!copy_button) {
193185
report_initialization_error({
194-
function_name: 'chatgpt.perform_copy',
186+
function_name: 'chatgpt.get_copy_button',
195187
log_message: 'Copy button not found'
196188
})
197-
return
189+
return null
198190
}
199-
copy_button.click()
200-
},
201-
insert_button: (f, b) =>
202-
f.insertBefore(b, f.children[f.children.length])
191+
return copy_button
192+
}
203193
})
204194
}
205195

@@ -211,7 +201,7 @@ export const chatgpt: Chatbot = {
211201
),
212202
footer_selector:
213203
'div[data-talvt-turn-state="complete"] > div:last-child > div',
214-
add_buttons: params.inject_button ? add_buttons : undefined
204+
bind_buttons: params.bind_copy ? bind_buttons : undefined
215205
})
216206
}
217207
}

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Chatbot } from '../types/chatbot'
22
import { CHATBOTS } from '@shared/constants/chatbots'
33
import {
4-
add_apply_response_button,
4+
bind_copy_button,
55
observe_for_responses
6-
} from '../utils/add-apply-response-button'
6+
} from '../utils/bind-copy-button'
77
import { report_initialization_error } from '../utils/report-initialization-error'
88

99
export const claude: Chatbot = {
@@ -81,37 +81,35 @@ export const claude: Chatbot = {
8181
input_element.focus()
8282
},
8383
setup_observer: (params) => {
84-
const add_buttons = (footer: Element) => {
85-
add_apply_response_button({
84+
const bind_buttons = (footer: Element) => {
85+
bind_copy_button({
8686
client_id: params.client_id,
8787
raw_instructions: params.raw_instructions,
8888
footer,
89-
perform_copy: (f) => {
89+
get_copy_button: (f) => {
9090
const copy_button = f.querySelector(
91-
'button:nth-child(2)'
91+
'button:first-child'
9292
) as HTMLElement
9393
if (!copy_button) {
9494
report_initialization_error({
95-
function_name: 'claude.perform_copy',
95+
function_name: 'claude.get_copy_button',
9696
log_message: 'Copy button not found'
9797
})
98-
return
98+
return null
9999
}
100-
copy_button.click()
101-
},
102-
insert_button: (f, b) => f.insertBefore(b, f.children[0])
100+
return copy_button
101+
}
103102
})
104103
}
105104

106-
const footer_selector =
107-
'div[data-cds="AssistantMessage"] > div:last-child > div'
105+
const footer_selector = 'div[data-testid="message-actions"]'
108106

109107
observe_for_responses({
110108
chatbot_name: 'Claude',
111109
is_generating: () =>
112110
!!document.querySelector('button[data-testid="chat-input-stop"]'),
113111
footer_selector,
114-
add_buttons: params.inject_button ? add_buttons : undefined
112+
bind_buttons: params.bind_copy ? bind_buttons : undefined
115113
})
116114
}
117115
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { CHATBOTS } from '@shared/constants/chatbots'
22
import { Chatbot } from '../types/chatbot'
33
import {
4-
add_apply_response_button,
4+
bind_copy_button,
55
observe_for_responses
6-
} from '../utils/add-apply-response-button'
6+
} from '../utils/bind-copy-button'
77
import { report_initialization_error } from '../utils/report-initialization-error'
88

99
export const copilot: Chatbot = {
@@ -133,25 +133,24 @@ export const copilot: Chatbot = {
133133
)
134134
},
135135
setup_observer: (params) => {
136-
const add_buttons = (footer: Element) => {
137-
add_apply_response_button({
136+
const bind_buttons = (footer: Element) => {
137+
bind_copy_button({
138138
client_id: params.client_id,
139139
raw_instructions: params.raw_instructions,
140140
footer,
141-
perform_copy: (f) => {
141+
get_copy_button: (f) => {
142142
const copy_button = f.querySelector(
143143
'button[data-testid="CopyButtonTestId"]'
144144
) as HTMLElement
145145
if (!copy_button) {
146146
report_initialization_error({
147-
function_name: 'copilot.perform_copy',
147+
function_name: 'copilot.get_copy_button',
148148
log_message: 'Copy button not found'
149149
})
150-
return
150+
return null
151151
}
152-
copy_button.click()
153-
},
154-
insert_button: (f, b) => f.insertBefore(b, f.firstChild)
152+
return copy_button
153+
}
155154
})
156155
}
157156

@@ -161,7 +160,7 @@ export const copilot: Chatbot = {
161160
!!document.querySelector('button svg rect[rx="2.5"]'),
162161
footer_selector:
163162
'.fai-CopilotMessage__actions > div > div > div > div > div',
164-
add_buttons: params.inject_button ? add_buttons : undefined
163+
bind_buttons: params.bind_copy ? bind_buttons : undefined
165164
})
166165
}
167166
}

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { CHATBOTS } from '@shared/constants/chatbots'
22
import { Chatbot } from '../types/chatbot'
33
import {
4-
add_apply_response_button,
4+
bind_copy_button,
55
observe_for_responses
6-
} from '../utils/add-apply-response-button'
6+
} from '../utils/bind-copy-button'
77
import { report_initialization_error } from '../utils/report-initialization-error'
88

99
export const deepseek: Chatbot = {
@@ -95,33 +95,24 @@ export const deepseek: Chatbot = {
9595
input_element.dispatchEvent(new Event('input', { bubbles: true }))
9696
},
9797
setup_observer: (params) => {
98-
const add_buttons = (footer: Element) => {
99-
add_apply_response_button({
98+
const bind_buttons = (footer: Element) => {
99+
bind_copy_button({
100100
client_id: params.client_id,
101101
raw_instructions: params.raw_instructions,
102102
footer,
103-
perform_copy: (f) => {
103+
get_copy_button: (f) => {
104104
const copy_button = f.querySelector(
105105
'div[role="button"]'
106106
) as HTMLElement
107107
if (!copy_button) {
108108
report_initialization_error({
109-
function_name: 'deepseek.perform_copy',
109+
function_name: 'deepseek.get_copy_button',
110110
log_message: 'Copy button not found'
111111
})
112-
return
113-
}
114-
if (!copy_button) {
115-
report_initialization_error({
116-
function_name: 'deepseek.perform_copy',
117-
log_message: 'Copy button not found'
118-
})
119-
return
112+
return null
120113
}
121-
copy_button.click()
122-
},
123-
insert_button: (f, b) =>
124-
f.insertBefore(b, f.children[f.children.length])
114+
return copy_button
115+
}
125116
})
126117
}
127118

@@ -132,7 +123,7 @@ export const deepseek: Chatbot = {
132123
'path[d="M2 4.88C2 3.68009 2 3.08013 2.30557 2.65954C2.40426 2.52371 2.52371 2.40426 2.65954 2.30557C3.08013 2 3.68009 2 4.88 2H11.12C12.3199 2 12.9199 2 13.3405 2.30557C13.4763 2.40426 13.5957 2.52371 13.6944 2.65954C14 3.08013 14 3.68009 14 4.88V11.12C14 12.3199 14 12.9199 13.6944 13.3405C13.5957 13.4763 13.4763 13.5957 13.3405 13.6944C12.9199 14 12.3199 14 11.12 14H4.88C3.68009 14 3.08013 14 2.65954 13.6944C2.52371 13.5957 2.40426 13.4763 2.30557 13.3405C2 12.9199 2 12.3199 2 11.12V4.88Z"]'
133124
),
134125
footer_selector: 'div.ds-message + div > div.ds-flex',
135-
add_buttons: params.inject_button ? add_buttons : undefined
126+
bind_buttons: params.bind_copy ? bind_buttons : undefined
136127
})
137128
}
138129
}

0 commit comments

Comments
 (0)