部落格園美化:JS實現頁面複製內容時自動新增版權資訊
阿新 • • 發佈:2021-06-20
在頁尾HTML中新增以下程式碼:
<script type="text/javascript"> var selfLogin = false; var follow = $('#profile_block').find('#p_b_follow'); var block = follow.length >= 1 ? follow[0] : ""; var followText = (block != null && block != undefined) ? block.innerHTML : ""; if(followText == "") selfLogin = true; var pageInfo = '\r\n---------------------\r\n著作權歸作者所有。\r\n' + '商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。\r\n' + '作者:Shu-How Z\r\n源地址:' + document.location.href + '\r\n來源:部落格園cnblogs\r\n© 版權宣告:本文為博主原創文章,轉載請附上博文連結!'; document.addEventListener('copy', function (ev) { var targetHTML = $.trim($(ev.target).html()); // console.log("targetHTML:" + targetHTML) if (targetHTML == "") return; if (!(targetHTML.startsWith('<table class="hljs-ln"') && targetHTML.endsWith('</table>')) || !selfLogin) // isLogined { var selected = window.getSelection(); var selectedText = selected.toString(); let copyRightStr = (selectedText + pageInfo).replace(/\n/g, '\r\n'); // Solve the line breaks conversion issue ev.clipboardData.setData('text/plain', `${copyRightStr}`); ev.preventDefault(); } }); </script>