3

Im trying to write a chrome extension that automatically writes text in the text box when a user presses a button. Im pretty sure the website I am trying to make the chrome extension work on is using a lexical editor and react.

Does anyone know how I can input text into a lexical editor from its DOM element?

Ive tried sending Keypress events to the lexical editor element and changing its inner text without any luck.

3
  • Please provide enough code so others can better understand or reproduce the problem.
    – Community Bot
    Jun 1, 2023 at 21:39
  • I think this is what you are looking for: stackoverflow.com/questions/73316417/… Jun 13, 2023 at 17:55
  • can you programmatically place a focus on input and send keyboard events?
    – Viktor M
    Apr 16 at 14:32

1 Answer 1

0

To simulate user input in the editor, we can use InputEvent API to dispatch the appropriate events.

const lexicalEditor = document.querySelector('editor-selector');

const inputEvent = new InputEvent('input', {
  data: 'Your text here',
  inputType: 'insertText',
  dataTransfer: null,
  isComposing: false,
  bubbles: true,
});
lexicalEditor.dispatchEvent(inputEvent);

Tested on Facebook (automated facebook post through chrome extension)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.