javascript自动复制文本到剪贴板

时间:2022-11-09 10:44:39

I'm trying to copy content to clipboard without user action but this doesn't work. is because firefox detect the origin of the action ? (click() function and not user real clic)

我试图在没有用户操作的情况下将内容复制到剪贴板,但这不起作用。是因为firefox检测到动作的来源? (click()函数而不是用户真实clic)

https://jsfiddle.net/ukj871dc/

<div id='div'>some text</div>

$("<button id='clickme'>clickme</button>").insertAfter( "#div" );
$("<textarea id='temptext'></textarea>").insertAfter( "#clickme" );
$("#temptext").css({"position": "fixed", "bottom": "0px", "left": "0px", "opacity": "0"});

// next one doesn't work
setTimeout(function () {document.getElementById("clickme").click();}, 2000);

$("#clickme").click(function(){
var textToCopy = $('#div').text();
$('#temptext').val(textToCopy);
$('#temptext').select();
document.execCommand('copy');                                     
});

It's just for personal interest don't bother long explanation you could give me links to documentation as well

这只是为了个人兴趣,不要费心长的解释,你也可以给我链接到文档

1 个解决方案

#1


0  

If you're using Firefox pre-41, you need to enable clipboard support manually in the user.js preference file. Reference: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Browser_compatibility

如果您使用的是Firefox 41之前的版本,则需要在user.js首选项文件中手动启用剪贴板支持。参考:https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Browser_compatibility

#1


0  

If you're using Firefox pre-41, you need to enable clipboard support manually in the user.js preference file. Reference: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Browser_compatibility

如果您使用的是Firefox 41之前的版本,则需要在user.js首选项文件中手动启用剪贴板支持。参考:https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Browser_compatibility