Skip to content

Commit 882721d

Browse files
committed
Copilot fixed double input
1 parent a92e11f commit 882721d

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

buttons-clicking-copilot.js

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function processCopilotCustomSendButtonClick(event, customText, autoSend) {
1212

1313
const injectionTargets = window.InjectionTargetsOnWebsite;
1414
const editorSelectors = injectionTargets.selectors.editors;
15-
const editorArea = editorSelectors.reduce((found, selector) =>
15+
const editorArea = editorSelectors.reduce((found, selector) =>
1616
found || document.querySelector(selector), null);
1717

1818
if (!editorArea) {
@@ -35,7 +35,7 @@ function processCopilotCustomSendButtonClick(event, customText, autoSend) {
3535
Object.getPrototypeOf(element),
3636
'value'
3737
)?.set;
38-
38+
3939
if (nativeInputValueSetter) {
4040
nativeInputValueSetter.call(element, text);
4141
element.dispatchEvent(new Event('input', { bubbles: true }));
@@ -50,31 +50,19 @@ function processCopilotCustomSendButtonClick(event, customText, autoSend) {
5050
.map(selector => document.querySelector(selector))
5151
.filter(Boolean);
5252

53-
logConCgp(sendButtons.length ?
54-
'[buttons] Send buttons located.' :
53+
logConCgp(sendButtons.length ?
54+
'[buttons] Send buttons located.' :
5555
'[buttons] Send buttons not found using dynamic selectors.');
5656

5757
return sendButtons;
5858
};
5959

60-
let originalSendButtons = locateSendButtons();
61-
6260
const handleSendButtons = (sendButtons) => {
6361
if (!sendButtons.length) {
6462
logConCgp('[buttons] Send buttons are not available to handle.');
6563
return;
6664
}
6765

68-
const existingText = editorArea.value ?? '';
69-
const newText = `${existingText}${customText}`;
70-
71-
setEditorValueDirectly(editorArea, newText);
72-
73-
if (editorArea.setSelectionRange) {
74-
editorArea.setSelectionRange(newText.length, newText.length);
75-
logConCgp('[buttons] Cursor moved to the end of the editor.');
76-
}
77-
7866
if (globalMaxExtensionConfig.globalAutoSendEnabled && autoSend) {
7967
logConCgp('[buttons] Auto-send is enabled. Starting auto-send process.');
8068
startAutoSend([sendButtons[0]], editorArea);
@@ -114,43 +102,54 @@ function processCopilotCustomSendButtonClick(event, customText, autoSend) {
114102
const handleMessageInsertion = () => {
115103
const initialState = isEditorInInitialState(editorArea);
116104

105+
// Step 1: Consolidate text insertion logic to prevent duplication.
117106
if (initialState) {
107+
// If editor is empty, just set the text.
118108
setEditorValueDirectly(editorArea, customText);
109+
} else {
110+
// If editor has content, append the new text.
111+
const existingText = editorArea.value ?? '';
112+
const newText = `${existingText}${customText}`;
113+
setEditorValueDirectly(editorArea, newText);
114+
}
119115

120-
if (editorArea.setSelectionRange) {
121-
editorArea.setSelectionRange(customText.length, customText.length);
122-
}
116+
// Step 2: Move cursor to the end after text is set.
117+
const finalLength = editorArea.value.length;
118+
if (editorArea.setSelectionRange) {
119+
editorArea.setSelectionRange(finalLength, finalLength);
120+
logConCgp('[buttons] Cursor moved to the end of the editor.');
121+
}
123122

124-
originalSendButtons = locateSendButtons();
125-
126-
if (!originalSendButtons.length) {
127-
const observer = new MutationObserver((mutations, obs) => {
128-
originalSendButtons = locateSendButtons();
129-
if (originalSendButtons.length) {
130-
handleSendButtons(originalSendButtons);
131-
obs.disconnect();
132-
logConCgp('[buttons] Send buttons detected and observer disconnected.');
133-
}
134-
});
135-
136-
observer.observe(document.body, {
137-
childList: true,
138-
subtree: true
139-
});
140-
141-
setTimeout(() => {
142-
if (!window.autoSendInterval) {
143-
observer.disconnect();
144-
logConCgp('[buttons] MutationObserver disconnected after timeout.');
145-
}
146-
}, 5000);
147-
} else {
148-
handleSendButtons(originalSendButtons);
149-
}
123+
// Step 3: Locate send buttons and handle auto-sending.
124+
let sendButtons = locateSendButtons();
125+
if (sendButtons.length) {
126+
handleSendButtons(sendButtons);
150127
} else {
151-
handleSendButtons(originalSendButtons);
128+
// If buttons aren't ready, wait for them with an observer.
129+
const observer = new MutationObserver((mutations, obs) => {
130+
const foundButtons = locateSendButtons();
131+
if (foundButtons.length) {
132+
handleSendButtons(foundButtons);
133+
obs.disconnect();
134+
logConCgp('[buttons] Send buttons detected and observer disconnected.');
135+
}
136+
});
137+
138+
observer.observe(document.body, {
139+
childList: true,
140+
subtree: true
141+
});
142+
143+
// Timeout to prevent the observer from running indefinitely.
144+
setTimeout(() => {
145+
if (!window.autoSendInterval) {
146+
observer.disconnect();
147+
logConCgp('[buttons] MutationObserver disconnected after timeout.');
148+
}
149+
}, 5000);
152150
}
153151
};
154152

155153
handleMessageInsertion();
156154
}
155+

0 commit comments

Comments
 (0)