function getSelectionHtml() {
    var sel = document.getSelection();
    var rgnCount = sel.rangeCount;
    var container = document.createElement("DIV");
    for (var i = 0; i < rgnCount; i++) {
        var rgn = sel.getRangeAt(i);
        var contents = rgn.cloneContents();
        container.appendChild(contents);
    }
    document.body.appendChild(container);
    var html = container.innerHTML;
    document.body.removeChild(container);
    return html;
}
getSelectionHtml();