0

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:

  1. selects some text on a page (it can include many DOM elements)
  2. triggers text wrapping by clicking a right-click menu button
  3. CORS with the selected text are performed to our server
  4. a text with law references wrapped in links is returned
  5. 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.

Vasily
  • 1,858
  • 1
  • 21
  • 34

1 Answers1

0

I'm not quite sure what you are asking, but from what I understand you want to replace DOM elements with other DOM elements. Look into innerHTML and/or createElement.

Isaac Wasserman
  • 1,461
  • 4
  • 19
  • 39
  • Yes, set of DOM elements with HTML. As I understand innerHTML just sets HTML into an element, but I need replace arbitrary set of DOM elements (which have been selected, some of them might be even not complete - e.g. if a user selected just a piece of a paragraph) . – Vasily May 25 '15 at 14:46
  • Refer to this http://stackoverflow.com/questions/6328718/wrapping-a-selected-text-node-with-span just use instead of – Isaac Wasserman May 25 '15 at 14:50
  • The question is about wrapping selected text, but not about replacing it, thanks anyway. – Vasily May 25 '15 at 14:57