Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's the concept, with some code to get you started. Every time text is selected in the page, append that text to a hidden textarea on the page and then select the textarea. Then, wrap the original selection in a span to make it look selected. This way, you have the appearance of a selection, and since the hidden textarea is actually selected, when the user hits "ctrl+c" they are copying the text from the textarea.</p> <p>To get the full functionality you are looking for, you'll probably want to extend this. Sniff keys for "ctrl+a" (for select all). I don't think you'll be able to override right-click behavior... at least not easily or elegantly. But this much is at least a proof of concept for you to run with.</p> <pre><code>window.onload = init; function init() { document.getElementById("hidden").value = ""; document.body.ondblclick = interceptSelection; document.body.onmouseup = interceptSelection; } function interceptSelection(e) { e = e || window.event; var target = e.target || e.srcElement; var hidden = document.getElementById("hidden"); if (target == hidden) return; var range, text; if (window.getSelection) { var sel = getSelection(); if (sel.rangeCount == 0) return; range = getSelection().getRangeAt(0); } else if (document.selection &amp;&amp; document.selection.createRange) { range = document.selection.createRange(); } text = "text" in range ? range.text : range.toString(); if (text) { if (range.surroundContents) { var span = document.createElement("span"); span.className = "selection"; range.surroundContents(span); } else if (range.pasteHTML) { range.pasteHTML("&lt;span class=\"selection\"&gt;" + text + "&lt;/span&gt;") } hidden.value += text; } hidden.select(); } </code></pre> <p>Here's the css I used in my test to hide the textarea and style the selected text:</p> <pre><code>#hidden { position: fixed; top: -100%; } .selection { background-color: Highlight; color: HighlightText; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload