function injectMarkers(){ var selection = window.getSelection() ,anchorNode = selection.anchorNode ,focusNode = selection.focusNode ,startMarker = 'STARTBEGINSNOW' ,endMarker = 'ENDBEGINSNOW' ,startTag = '<span id="citationalStart" style="width: 0px; display: inline-block;"> YEA</span>' ,endTag = '<span id="citationalEnd" style="width: 0px; display: inline-block;"> YEA</span>' ,anchorText = '' ,focusText = '' ,anchorHTML = '' ,focusHTML = '' ,range = selection.getRangeAt(0) ,asText = range.toString() ,base = 'http://localhost:3000'; if(anchorNode === focusNode){ anchorText = anchorNode.data; anchorText = focusText = anchorText.substring(0, selection.anchorOffset) + startMarker + anchorText.substring(selection.anchorOffset, selection.focusOffset) + endMarker + focusText.substring(selection.focusOffset); } else { anchorText = anchorNode.data; anchorText = anchorText.substring(0, selection.anchorOffset) + startMarker + anchorText.substring(selection.anchorOffset); focusText = selection.focusNode.data; focusText = focusText.substring(0, selection.focusOffset) + endMarker + focusText.substring(selection.focusOffset); } anchorNode.data = anchorText; focusNode.data = focusText; anchorHTML = anchorNode.parentNode.innerHTML; focusHTML = focusNode.parentNode.innerHTML; if($.contains(anchorNode.parentNode, focusNode.parentNode)){ anchorHTML = focusHTML = anchorHTML .replace(new RegExp(startMarker, 'gi'), startTag) .replace(new RegExp(endMarker, 'gi'), endTag); } else if($.contains(focusNode.parentNode, anchorNode.parentNode)){ anchorHTML = focusHTML = focusHTML .replace(new RegExp(startMarker, 'gi'), startTag) .replace(new RegExp(endMarker, 'gi'), endTag); } else if(anchorNode === focusNode){ anchorHTML = focusHTML = anchorHTML .replace(new RegExp(startMarker, 'gi'), startTag) .replace(new RegExp(endMarker, 'gi'), endTag); } else if(anchorNode.parentNode === focusNode.parentNode){ anchorHTML = focusHTML = anchorHTML .replace(new RegExp(startMarker, 'gi'), startTag) .replace(new RegExp(endMarker, 'gi'), endTag); } else { anchorHTML = anchorHTML.replace(new RegExp(startMarker, 'gi'), startTag); focusHTML = focusHTML.replace(new RegExp(endMarker, 'gi'), endTag); } if(anchorNode !== null && anchorNode.parentNode !== null) anchorNode.parentNode.innerHTML = anchorHTML; if(focusNode !== null && focusNode.parentNode !== null) focusNode.parentNode.innerHTML = focusHTML; } document.body.addEventListener('mouseup', injectMarkers, false);
<p><b>JavaScript</b> is an <a href="/wiki/Implementation" title="Implementation">implementation</a> of the <a href="/wiki/ECMAScript" title="ECMAScript">ECMAScript</a> language standard and is typically used to enable <a href="/wiki/Computer_programming" title="Computer programming">programmatic</a> access to computational objects within a host environment. It can be characterized as a <a href="/wiki/Prototype-based" title="Prototype-based" class="mw-redirect">prototype-based</a> <a href="/wiki/Object-oriented_programming" title="Object-oriented programming">object-oriented</a><sup id="cite_ref-ECMA-262_5-0" class="reference"><a href="#cite_note-ECMA-262-5"><span>[</span>6<span>]</span></a></sup> <a href="/wiki/Scripting_language" title="Scripting language">scripting language</a> that is <a href="/wiki/Dynamic_language" title="Dynamic language" class="mw-redirect">dynamic</a>, <a href="/wiki/Weak_typing" title="Weak typing">weakly typed</a> and has <a href="/wiki/First-class_function" title="First-class function">first-class functions</a>. It is also considered a <a href="/wiki/Functional_programming" title="Functional programming">functional programming</a> language<sup id="cite_ref-jsfunc_0-1" class="reference"><a href="#cite_note-jsfunc-0"><span>[</span>1<span>]</span></a></sup> like <a href="/wiki/Scheme_(programming_language)" title="Scheme (programming language)">Scheme</a> and <a href="/wiki/OCaml" title="OCaml" class="mw-redirect">OCaml</a> because it has <a href="/wiki/Closure_(computer_science)" title="Closure (computer science)" class="mw-redirect">closures</a> and supports <a href="/wiki/Higher-order_function" title="Higher-order function">higher-order functions</a>.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span>[</span>7<span>]</span></a></sup></p> <p>Another paragraph for testing, yeah yeah yeah.</p>