Skip to content

Instantly share code, notes, and snippets.

@luckyshot
Last active December 11, 2023 21:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckyshot/f1a91dc5e654c0cacac44c499ccec489 to your computer and use it in GitHub Desktop.
Save luckyshot/f1a91dc5e654c0cacac44c499ccec489 to your computer and use it in GitHub Desktop.
JavaScript - Add/Append custom text on copy to clipboard
/**
* Magic Copy
* This little script will append some text to the clipboard when a user copies text from the website
*
* WARNING: This feature is an anti-pattern and a bad usability practice in 99% of cases, use only in
* those situations where it can really benefit the user to have a link to the full resource
*/
document.addEventListener('copy', (event) => {
if (document.getSelection().toString().length < 10){ return; }
const pagelink = `\n${document.location.href}`;
event.clipboardData.setData('text', document.getSelection() + pagelink);
event.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment