const selection = window.getSelection();
alert(selection); // Shows a stringSelection API / notebook
Selection API
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const selectedText = selection.toString();const selObj = window.getSelection();
const range = selObj.getRangeAt(0);