JS让复制站点文章时自动添加版权

180it 2021-02-26 AM 1376℃ 0条
<script language="javascript" type="text/javascript">
jQuery(document).on('copy', function(e)
    {
      var selected = window.getSelection();
      var selectedText = selected.toString().replace(/\n/g, '<br>');  // Solve the line breaks conversion issue
      var pageInfo = '<br>---------(^-^)---------<br>'
                + '本文章原文链接<br>'+ document.location.href
                + '<br>来源:KIENG 博客<br>';
      var copyHolder = $('<div>', {id: 'temp', html: selectedText + copyFooter, style: {position: 'absolute', left: '-99999px'}});
        
      $('body').append(copyHolder);
      selected.selectAllChildren( copyHolder[0] );
      window.setTimeout(function() {
          copyHolder.remove();
      },0);
    });
</script>

代码二

<script type="text/javascript">
    var ua = navigator.userAgent.toLowerCase();
    if (window.ActiveXObject) {  /* 兼容 IE */
        document.body.oncopy = function () {
            event.returnValue = false;
            var selectedText = document.selection.createRange().text;
            var pageInfo = '<br>---------(^-^)---------<br>'
                + '本文章原文链接<br>'+ document.location.href
                + '<br>来源:KIENG 博客<br>';
            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
                + '<br>来源:KIENG 博客<br>';
            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>
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

JS让复制站点文章时自动添加版权