The window object offers the getSelection() function to allow scripts to retrieve highlighted text on the page. It will return the text highlighted to the calling code.
Example:
The code:
<!-- select something and then click button --> <button onclick="alert('You selected: '+window.getSelection()">Alert Selection</button>