Solution 1 :

if you want to get the data which was selected during cut/copy/paste you can use document.getSelection().getRangeAt(0).toString(); to get the clipboard data

document.addEventListener('copy', (event) => {
  const selection = document.getSelection().getRangeAt(0).toString();
  debugger;
  console.log(selection);
  event.preventDefault(selection);
});
document.addEventListener('cut', (event) => {
  const selection = document.getSelection().getRangeAt(0).toString();
  debugger;
  console.log(selection);
  event.preventDefault(selection);
});
document.addEventListener('paste', (event) => {
  const selection = document.getSelection().getRangeAt(0).toString();
  debugger;
  console.log(selection);
  event.preventDefault(selection);
});
AAA

Problem :

When I copy the letter A, it looks like an empty string is logged to the console. I expect A to be logged.

When I paste, the console successfully logs A.

document.addEventListener('copy', handler);
document.addEventListener('cut', handler);
document.addEventListener('paste', handler);

function handler(e) {
  console.log(e.clipboardData.getData('text/plain'));
}
A

Comments

Comment posted by “However, the handler cannot read the clipboard data.”

“However, the handler cannot read the clipboard data.”

Comment posted by Anthony

Try

By