const selection = window.getSelection();
const selRange = selection.getRangeAt(0);
// do stuff with the range
console.log(selection); // Selection objectWindow: getSelection() method / notebook
Window: getSelection() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.