0

I have wrote an javascript code to automate a process in a chatbot website . The program will fetch some random quote using a free api and will fill the textarea with that quote and will click send button in the chat application. Although the textarea is getting populated but the send button is can't be clicked both manually or programmatically unless a character is typed manually into the textarea.

Code:

const getNewQuote = async () =>
{
    
    //get Input Element and Send Button
    var inputField = document.getElementsByClassName("bot-textarea")[0];
    
    var sendbtn = document.querySelectorAll('[aria-label="Send"]')[0];
   
    //api for quotes
    var url="https://type.fit/api/quotes";    
    
    // fetch the data from api
    const response=await fetch(url);
    //convert response to json and store it in quotes array
    const allQuotes = await response.json();

    // Generates a random number between 0 and the length of the quotes array
    const indx = Math.floor(Math.random()*allQuotes.length);

    //Store the quote present at the randomly generated index
    const quote=allQuotes[indx].text;


    inputField.value = quote;
    sendbtn.click();
}
max tech
  • 69
  • 1
  • 9
  • because they are probably listening to either change or input events which is not triggered when you set the value directly. – epascarello Jun 05 '23 at 15:09
  • https://stackoverflow.com/questions/35659430/how-do-i-programmatically-trigger-an-input-event-without-jquery or https://stackoverflow.com/questions/2856513/how-can-i-trigger-an-onchange-event-manually – epascarello Jun 05 '23 at 15:10
  • @epascarello tried both the answer. didn't work. – max tech Jun 05 '23 at 15:14
  • So you did something like `inputField.value = quote; inputField.dispatchEvent(new Event('input', {bubbles:true}));` If that or change does not work then it really is a guessing game. Hard for us to guess what their code is doing. Maybe it is a keyup/down event. – epascarello Jun 05 '23 at 15:17
  • @epascarello Yes I tried it. Didn't work... the button only works when I click on the input field and add any character from keyboard manually. – max tech Jun 05 '23 at 15:29
  • Well you need to figure out what their code is doing. It is impossible for us to guess. As I stated maybe you need to fire a key event or maybe blur. There is nothing we can do since it is something in that code which we can not see. – epascarello Jun 05 '23 at 15:40

0 Answers0