How do I get the cursor position?

  node.js, question

Now I’m doing a message function. To achieve the effect of inserting pictures into qq chat input box, textarea can’t insert pictures.
So I used the contenteditable attribute of html5 to do it. But now the key is how to get the cursor position to determine which image to display in that position. However, it is impossible to get the index value and the displayed undefined by this method, but the cursor position can be obtained by changing to textarea. is there any solution?
图片描述

Ha ha; When I wrote this expressive comment function, I wrote the expression of tears. We must share our experience.
图片描述
You can experience it first:http://baijunyao.com/article/58

/**
 * 在textarea光标后插入内容
 * @param  string  str 需要插入的内容
 */
function insertHtmlAtCaret(str) {
    var sel, range;
    if(window.getSelection){
        sel = window.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0);
            range.deleteContents();
            var el = document.createElement("div");
            el.innerHTML = str;
            var frag = document.createDocumentFragment(), node, lastNode;
            while ( (node = el.firstChild) ) {
                lastNode = frag.appendChild(node);
            }
                range.insertNode(frag);
            if(lastNode){
                range = range.cloneRange();
                range.setStartAfter(lastNode);
                range.collapse(true);
                sel.removeAllRanges();
                sel.addRange(range);
            }
        }
    } else if (document.selection && document.selection.type != "Control") {
        document.selection.createRange().pasteHTML(str);
    }
}

JS for comments;
http://git.oschina.net/shuaib …

The source code can be downloaded and studied slowly:http://git.oschina.net/shuaib …