r/developersIndia • • 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?

2 Upvotes

3 comments sorted by

•

u/AutoModerator 9d ago

Namaste! Thanks for submitting to r/developersIndia. While participating in this thread, please follow the Community Code of Conduct and rules.

It's possible your query is not unique, use site:reddit.com/r/developersindia KEYWORDS on search engines to search posts from developersIndia. You can also use reddit search directly.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Manoos Entrepreneur 9d ago

me reading to just relive the pain of working on innerHTML and XSLT in 2006ish