I'm making a simple browser extension which is supposed to be used for links addition on a page. The extension should to find text occurrences with references to laws and wrap them in links to corresponding pages with texts of laws.
For example, the reference "chapter 1 of Some Law
" in a text "The topic covered in the chapter 1 of Some Law
" should be wrapped with link like this - "The topic covered in <a href="http://link-to-the-law.text">chapter 1 of Some Law</a>
"
A user interacts with the extension this way:
- selects some text on a page (it can include many DOM elements)
- triggers text wrapping by clicking a right-click menu button
- CORS with the selected text are performed to our server
- a text with law references wrapped in links is returned
- the selected text is replaced with the returned text
Now I've stuck with the step 5. I have a Selection object
and a raw received HTML with wrapped references.
Is it possible (and how if so) to replace a selected text (which actually is a bunch of DOM elements) with a received text? May be you have encountered some extensions/libraries/etc which acts similarily? Or may be you know a better way to do such a replacement? If so please let me know.