minor chatgpt fixes for react

This commit is contained in:
Mic 2026-04-21 00:18:00 +02:00
parent d007ed37e9
commit 0773f24867

View file

@ -41,18 +41,18 @@ async function chatgpt_sendMsg(msg, method ='') { // return -1 send button
console.error("[ThunderAI] Textarea not found!"); console.error("[ThunderAI] Textarea not found!");
return -2; return -2;
} }
// from sept 2024 // #prompt-textarea is a ProseMirror contenteditable. Place the caret at
// Remove existing content // the end of the inner paragraph and let execCommand('insertText') feed
while (textArea.firstChild) { // the text through the browser's input pipeline so PM can reconcile.
textArea.removeChild(textArea.firstChild); textArea.focus();
} const pmTarget = textArea.querySelector('p') || textArea;
// Parse msg as HTML and append its nodes const sel = window.getSelection();
const parser = new DOMParser(); const range = document.createRange();
const doc = parser.parseFromString(msg, 'text/html'); range.selectNodeContents(pmTarget);
Array.from(doc.body.childNodes).forEach(node => { range.collapse(false);
textArea.appendChild(node.cloneNode(true)); sel.removeAllRanges();
}); sel.addRange(range);
textArea.dispatchEvent(new Event('input', { bubbles: true })); document.execCommand('insertText', false, msg);
//wait for the button to change from the audio button to the send button (from nov-2024) //wait for the button to change from the audio button to the send button (from nov-2024)
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise(resolve => setTimeout(resolve, 1000));
let sendButton = document.querySelector('[data-testid="send-button"]') // pre-GPT-4o let sendButton = document.querySelector('[data-testid="send-button"]') // pre-GPT-4o
@ -66,8 +66,9 @@ async function chatgpt_sendMsg(msg, method ='') { // return -1 send button
const delaySend = setInterval(() => { const delaySend = setInterval(() => {
//console.log(">>>>>>>>>> sendButton disabled: " + sendButton?.hasAttribute('disabled')); //console.log(">>>>>>>>>> sendButton disabled: " + sendButton?.hasAttribute('disabled'));
if (!sendButton?.hasAttribute('disabled')) { // send msg if (!sendButton?.hasAttribute('disabled')) { // send msg
method.toLowerCase() == 'click' ? sendButton.click() // Always click the send button: a synthetic KeyboardEvent with
: textArea.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 13, bubbles: true })); // only keyCode:13 desyncs ProseMirror and crashes the React app.
sendButton.click();
clearInterval(delaySend); clearInterval(delaySend);
}else{ }else{
//console.error(">>>>>>>>>> The sendButton seems to be disabled! Trying again..."); //console.error(">>>>>>>>>> The sendButton seems to be disabled! Trying again...");
@ -911,9 +912,6 @@ function run(checkTab = null) {
} }
doLog("User not logged in, showing warning message."); doLog("User not logged in, showing warning message.");
alert(browser.i18n.getMessage("chatgpt_user_not_logged_in")); alert(browser.i18n.getMessage("chatgpt_user_not_logged_in"));
// we are not closing the window, because the user could try to log in
// doLog("User not logged in, closing window.");
// browser.runtime.sendMessage({command: "chatgpt_close", window_id: mztaWinId});
}else{ }else{
addCustomDiv(current_action,current_tabId,current_mailMessageId); addCustomDiv(current_action,current_tabId,current_mailMessageId);
(async () => { (async () => {