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

Request: Copy without onClick Event #218

Closed
mlucool opened this Issue Apr 2, 2016 · 5 comments

Comments

Projects
None yet
3 participants
@mlucool
Copy link

mlucool commented Apr 2, 2016

I would like to suggest the API allow for a programatic copy without a click event. The use case here is better integration with a framework like React where I want to use react's onClick bindings.

Optimally it would work as:

import {copy} from 'clipboard';
copy('my Text'); 

After playing with the code, it seems that this could be accomplished by a singleton Clipboard in the copy function that tracks nothing.

I gave this a quick attempt via adding the following, but the succeeded = document.execCommand(this.action) in clipboard-action.js returns false (does not throw). I am not sure why this would be the case when the normal clipboard via click works on the same page.

    copy(text) {
        if (this.clipboardAction) {
            this.clipboardAction = null;
        }

        this.clipboardAction = new ClipboardAction({
            action  : 'copy',
            target  : undefined,
            text    : text,
            trigger : undefined,
            emitter : this
        });
    }

Tested via:

setTimeout(function() {clipboard.copy('HHOLAAZ')}, 100);

Please let me know if you think this is a good idea and any suggestions on how to make it work. I can make the PR.

@zenorocha

This comment has been minimized.

Copy link
Owner

zenorocha commented Apr 4, 2016

I would love to have this too but due to security restrictions imposed by browser vendors, user interaction is required in order copy/cut to work. A simulated click event using JavaScript does not work as this would enable clipboard poisoning.

You might want to check https://www.npmjs.com/package/react-clipboard.js or https://www.npmjs.com/package/react-clip. Feel free to send a PR if you come up with something ;)

@zenorocha zenorocha closed this Apr 4, 2016

@mlucool

This comment has been minimized.

Copy link
Author

mlucool commented Apr 4, 2016

Thanks for the explanation. I had a feeling it was something like that. Can we make a note of this somewhere in the documentation?

@zenorocha

This comment has been minimized.

Copy link
Owner

zenorocha commented Apr 4, 2016

We already have this on the wiki (https://github.com/zenorocha/clipboard.js/wiki/Known-Limitations), but as soon as we redesign the site we can include it over there.

@zenorocha

This comment has been minimized.

Copy link
Owner

zenorocha commented Jan 23, 2018

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

Thanks!

@AJLoveChina

This comment has been minimized.

Copy link

AJLoveChina commented Aug 30, 2018

Thank you for this issue. @mlucool
The guide really really should apply this note to the readme.md guide, cause someone else may try to set data to clipboard without user interaction, just like me. But not working, so puzzling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment