Clipboard.js: Copy value to clipboard without user interaction

Created on 27 Jun 2016  路  1Comment  路  Source: zenorocha/clipboard.js

Actually, I konw a user iteraction is required(like a click).
But the click operation can be triggered by javascript but a real click operation, so I think this design is not really meanningful for developers(unless browsers make some limitation for some cases).
Like below:

function copyToClipboard(selector, text) {
        var clipboard = new Clipboard(selector,{
            text:function(trigger){
                return text;
            }
        });
        clipboard.on('success', function(e) {
            e.clearSelection();
            clipboard.destroy();
        });
        clipboard.on('error', function(e) {
            clipboard.destroy();
        });
        setTimeout(function(){
            $(selector).click();
        },100);
    }

So why don't we open the function 'copy to clipboard from variable' directly?

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings