By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Writing a character by keyPress in the textarea by JavaScript or jQuery (permission to send a message on chat.openai.com via Google Chrome extension) [duplicate]
JavaScript

Javascript – Writing a character by keyPress in the textarea by JavaScript or jQuery (permission to send a message on chat.openai.com via Google Chrome extension) [duplicate]

Admin
Last updated: 2024/01/07 at 6:49 AM
Admin
Share
2 Min Read
Writing a character by keyPress in the textarea by JavaScript or jQuery (permission to send a message on chat.openai.com via Google Chrome extension) [duplicate]

Problem:

This question already has answers here:

How can I enter data into a custom-handled input field?

(2 answers)
Closed 3 months ago.

I’m creating a Google Chrome extension that writes a message to a textarea on https://chat.openai.com/

Contents
Problem:Solution:

I can put text in the textarea, but I don’t have the option to send this text.

It looks like it might help if the character was inserted into the textarea via KeyPress (I can also use jQuery).

For some reasons I need to use this via Google Chrome extension and I can’t use the OpenAI API.


This JavaScript can put the text to the textarea:

document.getElementById('prompt-textarea').value = 'test';

text is inserted into the textarea, but I don’t see any option to send this message with JavaScript – the send button is disabled.

If I have this textarea focused and type any character on the real keyboard,
the button will stop being disabled and the message can be sent by JavaScript

document.getElementById('__next').getElementsByClassName('md:bottom-3')[0].click();

This is working well, but I need to not have to press any key on real keyboard.

It is not enough if I remove the disabled attribute with JavaScript

document.getElementById('__next').getElementsByClassName('md:bottom-3')[0].removeAttribute('disabled');

The button now appears to be clickable, but the message is not sent when clicked, still needs to be pressed key on keyboard.


As a possible solution I am looking for,
is to trigger an event as if some key on a real keyboard was pressed, which would really add a character to the textarea.

I tried e.g. JavaScript

document.getElementById('prompt-textarea').dispatchEvent(new KeyboardEvent('keypress',{'key':'a'}));

or jQuery

jQuery('#prompt-textarea').trigger({ type: 'keydown', which: 77 });

but it didn’t work – the character is not added to the textarea.
Can someone please advise?

Solution:

Thanks to user wOxxOm, he found a solution, using document.execCommand – I can just use this JavaScript and it works well:

document.getElementById('prompt-textarea').focus();
document.execCommand('insertText', false, 'test');

Instead of ‘test’, can using a variable containing the entire text for the textarea (prompt for OpenAI).

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Populating a React component with data from Firebase Javascript – Populating a React component with data from Firebase
Next Article compare elements of an array of Objects and merge based on a condition [duplicate] Javascript – compare elements of an array of Objects and merge based on a condition [duplicate]
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?