js教程:使用JS让复制站点文章时自动添加版权

<!--复制自动版权-->
<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>来源:菜鸟建站(www.newbii.cn)<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>来源:菜鸟建站(www.newbii.cn)<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>来源:菜鸟建站(www.newbii.cn)<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>

 

posted @ 2020-11-16 00:57  PHP菜鸟-小菜鸡  阅读(448)  评论(0编辑  收藏  举报
Top