Skip to content
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

Clipboard.js only works on click events #370

Closed
ddivecs opened this issue Jan 31, 2017 · 3 comments
Closed

Clipboard.js only works on click events #370

ddivecs opened this issue Jan 31, 2017 · 3 comments

Comments

@ddivecs
Copy link

ddivecs commented Jan 31, 2017

I understand that the clipboard api has security in place that requires user interaction and have read #127 and #189, but would it be possible to expand clipboard.js to work with other events besides .click (preferably, any arbitrary user event).

Specifically, it is useful in my app to respond to the keydown and context menu events, but currently clipboard only lets us respond to clicks. Also in these cases I would like to unbind from the click event in clipboardjs. Essentially, our click events handle selection in a grid, but should not necessarily trigger copy. Other interactions in our application cause copy.

@zenorocha
Copy link
Owner

Hey @ddivecs, this is a very nice feature suggestion but unfortunately the browser has some limitations in what is considered a user interaction. My advice to you is to use execCommand directly and see if it works for your particular case.

Thanks again for your input!

@lcw0622
Copy link

lcw0622 commented May 6, 2019

@zenorocha Hello, I'm very confused of this security restrictions because I had met many scenario that my program can only worked in click envent. Would you have something like document or key words can help me to get definitly description of it?
Thank you.

@raineorshine
Copy link

raineorshine commented Jun 4, 2021

This works for me in Chrome and Safari today:

/** Copies a string directly to the clipboard with a simulated button click. */
const copy = s => {
  const dummyButton = document.createElement('button')
  const clipboard = new ClipboardJS(dummyButton, { text: () => s })
  dummyButton.click()
}

// copy on keydown
window.addEventListener('keydown', e => {
  copy('Hello universe')
})

A simulated click event using JavaScript does not work as this would enable clipboard poisoning.

https://github.com/zenorocha/clipboard.js/wiki/Known-Limitations

Seems to not be a problem for keydown at least in Chrome and Safari.

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

4 participants