javascriptclipboard

event.preventDefault cannot block clipboard paste in chatgpt.com prompt box


I'm using following code to block paste to certain webpages:

document.addEventListener('paste', (event) => {
    event.preventDefault();
});

It works in most cases except for prompt box in https://chatgpt.com. Here is what I did step by step:

  1. Open https://chatgpt.com with chrome for Windows.
  2. Open console and run above codes.
  3. Copy some texts and paste to prompt box with CTL+V or right-click -> paste.
  4. Text is pasted successfully.

I wonder how it pastes text and is there any way to block it?

Update:

Thanks to @Unmitigated, ChatGPT works now with useCapture set to true.

document.addEventListener('paste', (event) => {
    event.preventDefault();
}, true);

However, I find more sites that don't work even with useCapture set to true, for example Gmail:

  1. open https://gmail.com with Chrome on Windows.
  2. Click "Compose" to create a new mail.
  3. Open console and run above codes.
  4. Copy some text and paste to email content.
  5. Text is pasted successfully.

Solution

  • You could use event capturing and also stop the event from propagating.

    document.addEventListener('paste', (event) => {
        event.preventDefault();
        event.stopPropagation();
    }, true);