Chrome Extension contenteditable get and set caret position

Posted by jwize on Stack Overflow See other posts from Stack Overflow or by jwize
Published on 2012-05-31T01:34:51Z Indexed on 2012/05/31 10:40 UTC
Read the original article Hit count: 291

I am creating an extension where I need to insert a link into the gmail window. I am able to create a simple extension button in the top of the window and execute code that does replaces all the links.

Now, I want to do add links in the contenteditable section as I type. It seems like there should be a simple way to replace text with a link in the document object model. If this is not true I need to do three things.

  1. Insert text from the current caret position
  2. Capture the caret position.
  3. Set the caret position by its offset.

I have tried using window.getSelection within the gmail context(that is a body element that is set to contenteditable and embedded inside an iframe in the gmail tab) to get the current range and position. The selection is always empty and contains no ranges regardless of whether text is selected or not.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about google-chrome