In general, you can copy selected text to the clipboard using document.execCommand("copy")
(part of the Clipboard API), and you can detect when the user selects text by listening for the selectionchange
event (part of the Selection API).
However, these features don't work together. At least in Chrome, the copy
command only works when a script is running as the direct result of a user interaction, like a click or keypress -- and selectionchange
events don't seem to count as user interactions. So you may be out of luck, unless there's something I've overlooked.