Skip to content

cut, copy, paste events handler issues #2738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
songhn233 opened this issue Oct 9, 2021 · 4 comments
Closed

cut, copy, paste events handler issues #2738

songhn233 opened this issue Oct 9, 2021 · 4 comments

Comments

@songhn233
Copy link
Member

The cut, copy prevention demo failed to show anything for event.clipboardData.getData('text/plain')

image

Change the code to the following and it will work

<input type="text" id="input">
<script>
  input.onpaste = function(event) {
    alert(event.type + ' - ' + event.clipboardData.getData('text/plain'));
    return false; // Equal to `event.preventDefault()`
  };
  input.oncut = input.oncopy = function(event) {
    alert(event.type + '-' + document.getSelection());
    return false;
  }
</script>

In addition, as mentioned above, We can also use event.preventDefault() to abort the action, so return false code should be added with a description equivalent to event.preventDefault().

Related issue: javascript-tutorial/zh.javascript.info#956

@joaquinelio
Copy link
Member

👍

mdn defines getData for paste only

i guess cut/copy falls under "ClipboardAPI: user safety restrictions" below
clipboard may have data from anywhere

@joaquinelio
Copy link
Member

joaquinelio commented Oct 9, 2021

i put a PR
single change

"Alternatively, the code below also prevents all such events and additionally shows what we are trying to cut/copy/paste:"

I'd suggest the code comment being " // false prevents changes" both returns

Your fix works, you may want to add a PR
regarding the getData issue, I dont know how deep the author wants to go

just suggestions, I'm nobody here :)

@songhn233
Copy link
Member Author

songhn233 commented Oct 10, 2021

"Alternatively, the code below also prevents all such events and additionally shows what we are trying to cut/copy/paste:"

I'd suggest the code comment being " // false prevents changes" both returns

Thank you for the suggestion, I will create a PR later.

@iliakan
Copy link
Member

iliakan commented Oct 10, 2021

Thank you, a good catch!
I rewrote this section. Please reopen if anything's wrong ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants