How to grab text that user typing on any webpage inside any message box and replace that text with some other text

Like grammerly I want to grab the area whee user is typing and want to grab the text and replace it with some other text. Due to diffrence arrangments of elemnts on various website I am not able to find a single way to grab that text. Is there any way to grab that text and to replace it with some other text?

I want a JS code that I can write in my chrome extension that can help me to grab text from any webpage.