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

Write to clipboard directly from javascript #329

Closed
gegomu opened this issue Oct 21, 2016 · 8 comments
Closed

Write to clipboard directly from javascript #329

gegomu opened this issue Oct 21, 2016 · 8 comments

Comments

@gegomu
Copy link

gegomu commented Oct 21, 2016

Minimal example

// This is what I need
var cpb = new Clipboard("#id");
cpb.copy("some text"); // Text is copied to clipboard

Expected behaviour

I wonder if it was possible to copy to clipboard directly from javascript, is it possible?

//-- this is my experiment
I try to delete the value copied to clipboard after a while, so I receive an event throw a function ...

var clipboard = new Clipboard("#myButton"); clipboard.on('success', function(e) { notifyCopy(e.text); } )

inside this function I try to delete the content of the clipboard
The function is (this is a test to see how to do it)

function notifyCopy(text){ var buttonVoid = document.getElementById("myButtonVoid"); if( buttonVoid .value == text ) return; setTimeout(function(){ buttonVoid .click(); }, 2000); }

myButtonVoid is a hidden button to force a copy event whith a empty value

This would delete the content of the clipboard overwriten the previos copied value

Actual behaviour

When the event copy is throw I get this error:

clipboard.min.js:7:6873
document.execCommand('cut'/'copy')It has been denied because it has not been called from within a
event handler user-generated short run.

So, I have two problems
1.- to write to the clipboad I need to create an extra element, add the clipboard copier to it and do some process, and finally it doesn't work
2.- I don't know if is there a direct copy method

Browsers affected

tested on Firefox 49.0.1

@djmattyg007
Copy link

I too was wondering about this recently. This library seems massively over-complicated when all you want to do is programmatically copy some text to the clipboard.

I ended up adapting the approach taken by this library into a very small class that have a hard dependency on binding events to buttons: https://github.com/djmattyg007/pictorials/blob/1c99a013fb9fa9fcd90d30c7f779e7f0fa345322/assets/js/clipboard.js

I'm not sure about the execCommand call being denied though, sorry.

@gegomu
Copy link
Author

gegomu commented Oct 22, 2016

oh, thank you very much
I Think the problem about the copy is for some security reason in Firefox. I've try in IE and I get the some problem. Also, I've try a solution similar to your new example an d the problem is similar.
You can not save anything to clipboard if it is not in a thread started by the user directly
Thanks one more time

@zenorocha
Copy link
Owner

You can't programmatically copy text to clipboard due to security restrictions imposed by browser vendors, user interaction is required in order copy/cut to work. A simulated click event also does not work as this would enable clipboard poisoning.

P.S.: If clipboard.js isn't the right solution for you, feel free to use another library or roll your own implementation :)

@djmattyg007
Copy link

I think the salient point is that someone may wish for more control over how text is copied to the clipboard upon a user action, rather than the single solution provided by this library of just binding event listeners to buttons directly.

@zenorocha
Copy link
Owner

I think the salient point is that someone may wish for more control over how text is copied to the clipboard upon a user action

If you want more control over how text is copied see the "Advanced options" in the docs.

rather than the single solution provided by this library of just binding event listeners to buttons directly.

As I said before, user interaction is required in order copy/cut to work. This is a browser requirement, not mine.

@adam-lynch
Copy link

This is too bad, but I completely understand. It would make sense to stick this in the docs right? It would save people time who are glancing over the docs and prevent people from getting annoyed at you @zenorocha 😄

@cesarpachon
Copy link

yeah most of the time when you face a new requirement (like implementing clipboard functionality) your first attempt is to look if someone had already implemented some reusable library for that, in order to save some time. I bet that's why many of us arrive at clipboard.js at first. noticing that it doesn't fit all user cases forces you go deep in how the clipboard mechanism work (the thing you were trying to avoid at first) and at the end, adapt, use other or roll your own implementation. In my use case, for a canvas-webgl game, I ended just adding a "copy" listener to the body, with a evt.clipboard.setData and document.execCommand('copy'). for the triggering. worked very well. as @adam-lynch suggest, highlight the expected use case in the doc will be of help.

@zenorocha
Copy link
Owner

Hey guys, I added a proposal for this feature. Can you check #518?

Thanks!

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

5 participants