C-Nedelcu / talk-to-chatgpt

Talk to ChatGPT AI using your voice and listen to its answers through a voice
GNU Affero General Public License v3.0
1.96k stars 335 forks source link

I fixed the extension not auto-sending messages #250

Open hoshizorista opened 2 weeks ago

hoshizorista commented 2 weeks ago

Hey guys, i noticed the extension its not auto-sending the message anymore after you speak to it, i managed to fixed it, there are some things that i need to delete since i was debugging it for a while, but it should be working now! if you are not really comfortable modifying the code feel free to check out my fork on my profile, has everything updated and working fine and you can just download it and install it

if the extension is not auto sending the message after you speak, open content.js and look for function CN_SendMessage(text) replace the entire function for this one instead:

(feel free to improve it or delete some trash i left there while debugging the issue)

function CN_SendMessage(text) {
    console.log("[CN_SendMessage] Trying to send message: " + text + " ");

    // Find the textarea either by class or id
    var textarea = jQuery('.overflow-hidden textarea');
    if (!textarea.length) {
        textarea = jQuery('#prompt-textarea');
        if (!textarea.length) {
            console.error('Textarea not found');
            return;
        }
    }

    // Focus on the textarea and simulate typing
    textarea.focus();
    var existingText = textarea.val();
    var fullText = existingText ? existingText + ' ' + text : text;
    var event = new Event('input', { bubbles: true });
    textarea.val(fullText)[0].dispatchEvent(event);

    // Adjust the height of the textarea
    var rows = Math.ceil(fullText.length / 88);
    var height = rows * 24;
    textarea.css('height', height + 'px');

    // Find the send button and enable it
    var sendButton = jQuery("[data-testid='fruitjuice-send-button']");
    if (sendButton.length) {
        sendButton.prop('disabled', false); // Force enable
        sendButton.removeAttr('disabled').removeClass('disabled');

        // Ensure the button is enabled and click it
        if (!sendButton.is(':disabled')) {
            sendButton[0].click(); // Attempt to click using DOM API
            console.log("[CN_SendMessage] Automatically clicking the send button.");
        } else {
            console.log("[CN_SendMessage] The send button is enabled but could not be clicked.");
        }
    } else {
        console.error("[CN_SendMessage] Send button not found.");
    }

    // Additional logic for speech recognition, if applicable
    if (CN_SPEECHREC) {
        clearTimeout(CN_TIMEOUT_KEEP_SPEECHREC_WORKING);
        CN_SPEECHREC.stop();
    } else {
        // Continue speech recognition
        clearTimeout(CN_TIMEOUT_KEEP_SPEECHREC_WORKING);
        CN_TIMEOUT_KEEP_SPEECHREC_WORKING = setTimeout(CN_KeepSpeechRecWorking, 100);
    }
}