You can do something like this, not quite the same as what you are after, I think? But may give you a start to further ideas.
<div>In cryptography, a keyed-hash message authentication code (HMAC) is a specific construction for calculating a message authentication code (MAC) involving a cryptographic hash function in combination with a secret cryptographic key. As with any MAC, it may be used to simultaneously verify both the data integrity and the authentication of a message. Any cryptographic hash function, such as MD5 or SHA-1, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMAC-MD5 or HMAC-SHA1 accordingly. The cryptographic strength of the HMAC depends upon the cryptographic strength of the underlying hash function, the size of its hash output, and on the size and quality of the key.</div>
<button id="get">Get Selected</button>
function getText() {
var selectedText
if (typeof window.getSelection === "function") {
selectedText = window.getSelection();
} else if (typeof document.getSelection === "function") {
selectedText = document.getSelection();
} else if (document.selection && typeof document.selection.createRange() === "function") {
selectedText = document.selection.createRange().text;
} else {
selectedText = "";
alert("No method to get selected text");
}
if (!selectedText || selectedText === "") {
if (document.activeElement.selectionStart) {
selectedText = document.activeElement.value.substring(
document.activeElement.selectionStart.document.activeElement.selectionEnd);
}
}
alert(selectedText);
}
document.getElementById("get").addEventListener("click", getText, false);
on jsfiddle
you can also see a further answer where I have expanded on this idea here on SO.
the author pulled the other question but here is the other jsfiddle
window.getSelection
Summary
Returns a selection object representing the range of text selected by
the user.
Specification
DOM Level 0. Not part of any standard.
It is expected to be specified in a new DOM Range spec
There is also a library called Rangy that is supposed to handle this kind of thin cross-browser, never tried it but you may want to take a look.
A cross-browser JavaScript range and selection library. It provides a
simple standards-based API for performing common DOM Range and
Selection tasks in all major browsers, abstracting away the wildly
different implementations of this functionality between Internet
Explorer up to and including version 8 and DOM-compliant browsers.