网页复制内容时自动添加版权链接信息

该日志由 samool 发表于 2020-06-19 23:56:00

JS 仿知乎复制文章末尾自动添加字符串
当别人复制你的文章内容时,会在文章末尾自动添加一串字符。
将此代码放置在body之前即可

<!--自动添加版权信息 -->
<script>
$("body").bind('copy', function (e) {
    if (typeof window.getSelection == "undefined") return; //IE8 及更老的版本不兼容
    
    var body_element = document.getElementsByTagName('body')[0];
    var selection = window.getSelection();
    
    //创建一个DIV的可见区域之外
    //并填写选定的文本
    var newdiv = document.createElement('div');
    newdiv.style.position = 'absolute';
    newdiv.style.left = '-99999px';
    body_element.appendChild(newdiv);
    newdiv.appendChild(selection.getRangeAt(0).cloneContents());
    
    //我们需要<pre>标签解决方案
    //其他的文本在<pre>失去了所有的行符!
    if (selection.getRangeAt(0).commonAncestorContainer.nodeName == "PRE") {
        newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
    }
    
    newdiv.innerHTML += "<br /><br />文章来自: 《傻猫日志》 <a href='"
    + document.location.href + "'>"
    + document.location.href + "</a>";
            
    selection.selectAllChildren(newdiv);
    window.setTimeout(function () { body_element.removeChild(newdiv); }, 200);
});
</script>   

该日志标签: 标签, 日志, 内容, 网页, , 链接, 代码, 信息, 复制, 文章, o

上一篇: 百度搜索自动提交网站内容
下一篇: JavaScript Array 数组常用函数

当前暂无评论 »

添加新评论 »