I'm looking for a way to programmatically paste using a button click. I've tried various things from adding an event listener, or execCommand('paste').
Asked
Active
Viewed 307 times
1 Answers
0
I think this question is a duplicate of: Paste clipboard content to input box on button click JS.
I do not believe there is cross-browser support for pasting the content's of a clipboard on a button click. Check out Can I use... for the Clipboard API. For Chrome 67, it says that, "Supports cut & copy events without a focused editable field, but does not fire paste with document.execCommand('paste')." For Firefox 61, it says, "Supports cut & copy events without a focused editable field, but not paste (presumably for security reasons)."
You can intercept paste events with something like:
document.addEventListener('paste', ev => {
console.log(ev.clipboardData.getData('text/plain'));
});
But you can't get the contents of the user's clipboard without the user choosing to paste.

mattbatman
- 442
- 1
- 3
- 8