Inject text in Twitter's inputfield

193 views
Skip to first unread message

Thibault LOUIS-LUCAS

unread,
Feb 9, 2021, 5:29:56 PM2/9/21
to Chromium Extensions
Hi,

I am trying to build a Chrome extension that generates texts on Twitter. I am struggling with injecting the text in the inputfield.

Here is how I do it:

  • If the user has already input some content, it works great
  • If the user has not already input content, it doesn't ...
To inject texts, I am using:
document.execCommand('insertText', false, 'blablabla');

However, on the second use case, it just doesn't work. The placeholder stays visible and the inputfied is broken.

See it in action: => https://www.loom.com/share/4bac993ba2cd4ee4a38fc209e751e323

Am I missing something? Is there some kind of JS variable?

Thanks

Teddy

unread,
Mar 3, 2021, 7:59:22 AM3/3/21
to Thibault LOUIS-LUCAS, Chromium Extensions
>To inject texts, I am using:
>document.execCommand('insertText', false, 'blablabla');

Pressing on your "Generate" button probably unfocuses the input field.

The `insertText` command:

> Inserts the given plain text at the insertion point (deletes selection).

(https://developer.mozilla.org/en-US/docs/Web/API/document/execCommand#commands)

If the input field doesn't have focus, then there's presumably no
insertion point at which to insert the text.

Furthermore, the `execCommand` API is described as deprecated:
https://developer.mozilla.org/en-US/docs/Web/API/document/execCommand#Deprecated

I'd recommend querying the input field DOM element with
`document.querySelector`, then inserting your text by setting
`textContent`
(https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent).

--










*"This email and any files transmitted with it are confidential
and intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify the
sender immediately"*
Reply all
Reply to author
Forward
0 new messages