mask2012 / MaskBlog

all blogs are in issues.
12 stars 5 forks source link

抖音引流价值百万的代码 #97

Open mask2012 opened 4 years ago

mask2012 commented 4 years ago

粘贴规律:

  1. 必须用户点击,不是滑动,才可以进行粘贴
  2. 点击后粘贴成功后就退出拷贝机制,允许用户复制自己的内容

必须是点击才能拷贝成功,所以需要一个钩子让用户点击,经过我的试验,普通浏览页面的滑动很难进去

var copyed = false      
function clipboardWrite(text) {
    if (copyed) { return }
    copyed = true
    var tempDiv = document.createElement("div")
    tempDiv.id = 'tempDiv';
    tempDiv.innerHTML = text;
    tempDiv.style.position = "absolute";
    tempDiv.style.left = "-9999px";
    document.body.appendChild(tempDiv);
    var target = document.getElementById('tempDiv');
    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents(target);
    selection.removeAllRanges();
    selection.addRange(range);
    var a = document.execCommand("Copy", "false", null);
    selection.removeAllRanges();
    console.log('a', a);
    // mobileConsole(new Date().getTime())
}
document.getElementsByTagName("body")[0].addEventListener("click", function () {
    clipboardWrite('555');
});