复制自动加入版权内容

Ginc
Ginc
管理员
66
文章
0
粉丝
建站开发2279阅读模式
前言

WordPress站点内容被别人复制是常有的事,可以给你的WordPress站点添加一个功能:内容被复制后,粘贴的时候自动在后面添加文章链接。

2022-11-18  本教程在知更鸟最新主题中失效,可能会出现粘贴内容为空。文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html

 文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html

复制自动加入版权内容

实现方法很简单,只需要复制下面代码,放到  footer.php  <body>标签内即可文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html

代码:文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html

<script type="text/javascript">
    var ua = navigator.userAgent.toLowerCase();
    if (window.ActiveXObject) 
        document.body.oncopy = function () {
            event.returnValue = false;
            var selectedText = document.selection.createRange().text;
            var pageInfo = '<br>---------(^-^)---------<br>'
                + '原文链接<br>'+ document.location.href
            clipboardData.setData('Text', selectedText.replace(/\n/g, '<br>') + pageInfo);
        }
    }
    else {
        function addCopyRight() {
            var body_element = document.getElementsByTagName('body')[0];
            var selection = window.getSelection();
            var pageInfo = '<br>---------(^-^)---------<br>'
                + '原文链接<br>'+ document.location.href
            var copyText = selection.toString().replace(/\n/g, '<br>') + pageInfo;  // Solve the line breaks conversion issue
            var newDiv = document.createElement('div');
            newDiv.style.position = 'absolute';
            newDiv.style.left = '-99999px';
            body_element.appendChild(newDiv);
            newDiv.innerHTML = copyText;
            selection.selectAllChildren(newDiv);
            window.setTimeout(function () {
                body_element.removeChild(newDiv);
            }, 0);
        }
        document.oncopy = addCopyRight;
    }
</script>

这段代码实现的方法有很多,可以直接引用,也可以做成JS文件再引用。文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html

本文代码可与   添加复制提示弹窗 | 今夕何夕兮 (www.ginc.site)   一起食用。文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html

效果图:文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html

本文引用

代码引用自CSDN博主 :BUG制造者:图图  地址 :https://bugapi.blog.csdn.net/?type=blog文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html

文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html文章源自今夕何夕兮-https://www.ginc.site/building-and-development748.html

转载请注明来自:今夕何夕兮
本站原创内容版权遵循 CC-BY-NC-SA4.0 协议授权,本站部分资源收集于网络,只做学习和交流使用,版权归原作者所有。

weinxin
提示:
若文章中图片、链接等信息出错,请及时反馈,博主将在第一时间更新。谢谢大家!
 
Ginc
  • 本文由 Ginc 发表于2022年9月28日
  • 转载请保留本文链接:复制自动加入版权内容:https://www.ginc.site/building-and-development748.html
评论  2  访客  2
    • TeacherDu
      TeacherDu

      代码貌似是通用的,放在Hexo里试试!

        • 今夕
          今夕

          @ TeacherDu 是的,这种放在页脚里的都时通用的

      匿名

      发表评论

      匿名网友
      确定

      拖动滑块以完成验证