Javascript 如何将正文中的文本内容复制到剪贴板
我需要将我身体中的所有文本复制到剪贴板, 以下是我到目前为止所做的尝试:Javascript 如何将正文中的文本内容复制到剪贴板,javascript,clipboard,chromium-embedded,Javascript,Clipboard,Chromium Embedded,我需要将我身体中的所有文本复制到剪贴板, 以下是我到目前为止所做的尝试: 选择文本节点,然后选择命令document.execCommand(“复制”) 选择文本节点,然后使用键盘调度程序: $("body").contents().filter(function(){return this.nodeType === 3;}).select(); document.body.dispatchEvent(new KeyboardEvent("keyup", {bubbles: true, can
- 选择文本节点,然后选择命令
document.execCommand(“复制”)
- 选择文本节点,然后使用键盘调度程序:
$("body").contents().filter(function(){return this.nodeType === 3;}).select(); document.body.dispatchEvent(new KeyboardEvent("keyup", {bubbles: true, cancelable: false, key: "C", char: "C", ctrlKey: true}));
没有错误弹出。我在Chromium文档中读到,出于安全原因,copy命令被禁用。你知道如何解决这个问题吗?复制到剪贴板只会在真正的用户交互中起作用。如果没有真正的用户交互,它通常会失败。我相信这是为了安全措施。 所以把它挂在一个点击事件上。然后我还建议您使用这样的库来解决不同浏览器的问题,并允许您输入html变体和纯文本副本 如果使用clipboard.js,可以使用如下代码:
plaintext = "boo";
htmltext = "<strong>boo</strong>";
document.getElementById("copybutton").addEventListener('click', function() {
clipboard.copy({
'text/plain': plaintext,
'text/html': htmltext
}).then(
function(){
swal({
title: "Successfully copied",
text: "The thing has been put in your clipboard, happy pasting!",
type: "success",
closeOnConfirm:true,
confirmButtonText: "Ok",
timer: 1200
});
},
function(err){
window.prompt("Something went wrong with automatically copying the data to clipboard.\nPlease press CTRC + C to copy the data",plaintext );
});
}
}
plaintext=“boo”;
htmltext=“boo”;
document.getElementById(“copybutton”).addEventListener('click',function(){
剪贴板。复制({
“文本/纯文本”:纯文本,
“text/html”:htmltext
}).那么(
函数(){
游泳({
标题:“成功复制”,
文字:“东西已经放在你的剪贴板上了,快乐粘贴!”,
键入:“成功”,
closeOnConfirm:true,
confirmButtonText:“确定”,
计时器:1200
});
},
功能(err){
window.prompt(“自动将数据复制到剪贴板时出错。\n请按CTRC+C复制数据”,纯文本);
});
}
}