Skip to content

Instantly share code, notes, and snippets.

@abdullah
Last active April 30, 2024 11:00
Show Gist options
  • Save abdullah/ffa8d9c73ced0bc30296765176236892 to your computer and use it in GitHub Desktop.
Save abdullah/ffa8d9c73ced0bc30296765176236892 to your computer and use it in GitHub Desktop.
Selection End Event
let selectionEndTimeout = null,
event = new Event('selectionEnd');
document.addEventListener('selectionEnd', function (evt/*event*/) {
console.log("End selection!")
}, false);
[
"mouseup",
"selectionchange"
].map(e => {
document.addEventListener(e.toString(),(evt/*event*/)=>{
if (selectionEndTimeout && evt.type == "selectionchange") {
clearTimeout(selectionEndTimeout);
console.info('User Selection Changed');
}
selectionEndTimeout = setTimeout(function () {
if (evt.type == "mouseup" && window.getSelection().toString() != "") {
document.dispatchEvent(event);
}
}, 100);
})
})
@warren30815
Copy link

Work fine! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment