Skip to content

Instantly share code, notes, and snippets.

@juji
Last active July 25, 2024 03:28
Show Gist options
  • Save juji/2411d2e1b8ee2580933039e6e0e81da3 to your computer and use it in GitHub Desktop.
Save juji/2411d2e1b8ee2580933039e6e0e81da3 to your computer and use it in GitHub Desktop.
clean clipboard text
export function cleanClipboardText(e: ClipboardEvent){
const pastedText = e.clipboardData?.getData('text/plain');
return pastedText ? stripBom(pastedText) : ''
}
export function onPaste(e: ClipboardEvent){
e.preventDefault()
const sel = window.getSelection()
if(!sel) return;
if (!sel.rangeCount) return;
const data = cleanClipboardText(e)
sel.getRangeAt(0).insertNode(document.createTextNode(data));
sel.collapseToEnd();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment