kpie
kpie

Reputation: 11100

How do I put a string in the paste buffer using javaScript in meteor?

I am currently building a tree based discussion form. Each post or node in the tree has a unique Id and a button "getURL" which supplies a URL which when navigated to provides a view rooted at that given node.

At the moment the getURL button opens a new window and everything works swimmingly.

I would like to have the url put in the users paste buffer (as if they selected the url and typed control-c)

I understand that there are potential security vulnerabilities giving a site access to the paste buffer, but there must be a simple work around.

I tried using this http://ajaxian.com/archives/auto-copy-to-clipboard as seen How do I copy to the clipboard in JavaScript? but it did not work and realizing now that that post is nearly 10 years old I figured I may as well ask the question,

Is there an ES6 compliant approach to define copy(myStr) where running said function will put myStr in the paste buffer?

I have seen a number of packages like this one https://atmospherejs.com/zenorocha/clipboard But I don't want to have to add something to the dom just to put it in the paste buffer, is there any way to define a copy function I can just pass a string to in pure js w/o flash?

I'll be digging around the source for zenorocha/clipbaord looking for the answer but if you know it, it would be so appreciated!

Upvotes: 0

Views: 458

Answers (1)

JeremyK
JeremyK

Reputation: 3240

There is a nice writeup here about creating 'copy to clipboard' functionality.

The short answer is, use clipboard.js (zenorocha/clipboard uses this library) if you want a robust, general solution. However if you can live with limitations there are simpler solutions.

Upvotes: 1

Related Questions