Skip to content

Instantly share code, notes, and snippets.

@stcalica
Created November 10, 2023 01:13
Show Gist options
  • Save stcalica/3b6a12d595bcd74dea0bd7d8862ff8b4 to your computer and use it in GitHub Desktop.
Save stcalica/3b6a12d595bcd74dea0bd7d8862ff8b4 to your computer and use it in GitHub Desktop.
Appending data to copied text when there's a copy action
document.addEventListener('copy', function(event) {
// Get the selected text
var selectedText = window.getSelection().toString();
// Generate a nonce (unique identifier)
var nonce = 'my_unique_nonce';
// Append the nonce to the selected text
var modifiedText = selectedText + ' [Nonce: ' + nonce + ']';
// Copy the modified text to the clipboard
event.clipboardData.setData('text/plain', modifiedText);
// Prevent the default copy behavior
event.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment