get selected text js

JavaScript
// Get highlighted text this way:

window.getSelection().toString()
Source

Also in JavaScript: