JSFiddle - React, Tailwind, and code Playground

by codef0rmer

HTML

<iframe id='iframe' width='100%' height='50px' frameborder='1'></iframe>
<button>Grab and Wrap - Iframe</button>

JavaScript

// Range demo for a Blog
$(function () {
    // Rangy - Selection/Highlighting Test
    var iframe = $('#iframe')[0],
        iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
        iframeDoc.open();
        iframeDoc.write("<html><body>Please <b>select some</b> of this text and press the button</body></html>");
        iframeDoc.close();
});

function grabSelectedText() {
    var t = '', 
        window = $('iframe#bookViewer')[0].contentWindow.window, 
        document = $('iframe#bookViewer')[0].contentWindow.document,
        chucknorris = document.createElement("chucknorris");

    if (window.getSelection) {
        t = window.getSelection();
    } else if (document.getSelection) {
        t = document.getSelection();
    } else if (document.selection) {
        t = document.selection.createRange().text;
    }
    
    var range = t.getRangeAt(0).cloneRange();
    range.surroundContents(chucknorris);
    t.removeAllRanges();
    t.addRange(range);
    
    return t;
}