The Window.getSelection() method returns a Selection object representing the range of text selected by the user or the current position of the caret.

Window.getSelection() - Web APIs | MDN