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:
- Open
https://chatgpt.com
with chrome for Windows. - Open console and run above codes.
- Copy some texts and paste to prompt box with CTL+V or right-click -> paste.
- 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:
- open
https://gmail.com
with Chrome on Windows. - Click “Compose” to create a new mail.
- Open console and run above codes.
- Copy some text and paste to email content.
- Text is pasted successfully.
You could use event capturing and also stop the event from propagating.
document.addEventListener('paste', (event) => {
event.preventDefault();
event.stopPropagation();
}, true);
3