Last active
December 11, 2023 21:00
-
-
Save luckyshot/f1a91dc5e654c0cacac44c499ccec489 to your computer and use it in GitHub Desktop.
JavaScript - Add/Append custom text on copy to clipboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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