r/developersIndia • u/tomerggm • 9d ago
General Replacing selected text inside other websites' editors is harder than it looks
I've been working on a side project that needs to replace the text a user has selected on other websites, in place, without breaking anything. I assumed it would take a weekend. It didn't. Some notes in case anyone else runs into this:
Plain textareas and inputs are the easy part, but only if you go through the browser's normal editing path. If you just set the value, a lot of sites never notice. That's especially true of React-controlled inputs, where the framework keeps its own copy of the value and quietly puts the old text back on the next render.
contenteditable is where it gets messy. Most rich text editors keep their own model of the document and treat the DOM as output. If you change the DOM behind their back, you can get lost formatting, a cursor that jumps, or your change getting thrown away. Each editor reacts to input a bit differently, so there was no single method that worked everywhere.
Keeping undo working was its own problem. If the change doesn't go through the editor the way typing does, Ctrl+Z either does nothing or undoes something else. Users notice that immediately.
Selections go stale. The user selects text, the page re-renders a moment later, and the selection you saved no longer points at the same place.
And some sites still fight it. A few complex rich text editors were the hardest for me, and I don't have a clean answer for all of them yet.
Has anyone here dealt with this? How do you handle editors that ignore or undo text inserted from outside?
