Document.execCommand()仅在开发人员工具中有效

时间:2018-07-25 22:51:35

标签: javascript html anchor developer-tools execcommand

我正在尝试将网页的所有链接复制到剪贴板。我正在尝试将所有锚标记连接到一个字符串中,将该字符串放入输入字段中,然后通过document.execCommand("copy")复制它,但是以某种方式document.execCommand("copy")仅在浏览器开发人员工具中有效。我希望它能在网页中加载的脚本中工作。请帮助我,谢谢。

var
body = document.querySelector("body"),
input = document.createElement("textarea"),
a = document.getElementsByTagName("a"),
list = [],
anchor = document.createElement("a");
for (let i = 0; i < a.length; i++){
    list.push(a[i]);
};
list = list.join("\r\n");
input.value = list;
input.setAttribute("readonly", "");
input.style = "position: absolute; left: -9999px;";
body.appendChild(input);
input.select();
document.execCommand('copy');
body.removeChild(input);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>test</title>
    </head>
<body>
    <a href="http://ali.com">sample link</a>
    <script src="script.js"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

需要注意的是,execCommand('copy')仅在用户调用动作的上下文中可靠地起作用。换句话说,如果要将数据复制到剪贴板,则应这样做,这是用户说单击按钮的副作用。

例如,您可以按照以下方式修改脚本-将调用移至click事件处理程序内的execCommand(),以实现所需的复制到剪贴板的行为:

var
body = document.querySelector("body"),
input = document.createElement("textarea"),
a = document.getElementsByTagName("a"),
anchor = document.createElement("a");

input.setAttribute("readonly", "");

// Added a copy button for the purpose of this demonstration
var copyButton = document.createElement("button")
copyButton.innerText = 'Copy'

// The event in which the copy to clip board will occour
copyButton.addEventListener('click', () => {

    // This code is in the context of a user 'click' action 
    var list = []
    for (let i = 0; i < a.length; i++){
        list.push(a[i]);
    };
    list = list.join("\r\n");

    body.appendChild(input);
    input.value = list;
    input.focus();
    input.select();

    // exec copy command is now being called in the context of 
    // a user invoked interaction (ie click), so it should work
    // as expected
    if(document.execCommand('copy')) {
        console.log('copy success')
    }
    else {
        console.log('copy failed')
    }
    body.removeChild(input);
})

body.appendChild(copyButton);

答案 1 :(得分:0)

当用户单击窗口时,可以使用Javascript执行复制功能。复制到剪贴板需要通过用户的动作(即单击)来触发。

<a href="http://ali.com">sample link</a>
<script>
var copied = false
window.onclick = function(e){
  if(!copied){
   copy();
   copied = true;
   e.target.focus();//focus back on target of click to prevent constant blurring of first click
   }
}
function copy(){
	 var
body = document.body,
input = document.createElement("textarea"),
a = document.getElementsByTagName("a"),
list = [],
anchor = document.createElement("a");
for (let i = 0; i < a.length; i++){
   list.push(a[i]);
};
list = list.join("\r\n");
input.value = list;
input.setAttribute("readonly", "true");
input.style = "position: absolute; left: -9999px;";
document.body.appendChild(input);
input.select();
document.execCommand('copy');
input.remove();
}
</script>
<button id="hidden" onClick="copy()" style="display: none;">
</button>
<br/>
<textarea placeholder="Paste the copied text"></textarea>