为博客添加复制文章自带版权信息功能

前言

该js的效果就是在别人复制你文章时弹窗提示兵自动添加一个版权信息,对于复制者来说也算是个提示,不过要防止别人复制是不可能的,这个大家都知道。该js只在typecho博客上测试过,理论上其他平台的博客也可以用,毕竟是js嘛!好了,直接上干货。

js代码

<script>
document.body.addEventListener('copy', function (e) {
    if (window.getSelection().toString() && window.getSelection().toString().length > 42) {
        setClipboardText(e);
        alert('商业转载请联系作者获得授权,非商业转载请注明出处,谢谢合作。');
    }
}); 
function setClipboardText(event) {
    var clipboardData = event.clipboardData || window.clipboardData;
    if (clipboardData) {
        event.preventDefault();
        var htmlData = ''
            + '著作权归作者所有。<br>'
            + '商业转载请联系作者获得授权,非商业转载请注明出处。<br>'
            + '作者:<?php $this->author() ?><br>'
            + '链接:' + window.location.href + '<br>'
            + '来源:<?php $this->options->siteUrl(); ?><br><br>'
            + window.getSelection().toString();
        var textData = ''
            + '著作权归作者所有。\n'
            + '商业转载请联系作者获得授权,非商业转载请注明出处。\n'
            + '作者:<?php $this->author() ?>\n'
            + '链接:' + window.location.href + '\n'
            + '来源:<?php $this->options->siteUrl(); ?>\n\n'
            + window.getSelection().toString();
 
        clipboardData.setData('text/html', htmlData);
        clipboardData.setData('text/plain',textData);
    }
}
</script>

使用方法

在博客主题的post.php里添加即可,其他博客系统请自行摸索#(想一想)

tag(s): 博客
show comments · back · home
Edit with markdown

仅有一条评论

  1. Kala Godley

    It is with sad regret to inform you StarDataGroup.com is shutting down.
    It has been a tough year all round and we decided to go out with a bang!

    Any group of databases listed below is $49 or $149 for all 16 databases in this one time offer.
    You can purchase it at www.StarDataGroup.com and view samples.

    LinkedIn Database
    43,535,433 LinkedIn RecordsUSA B2B Companies Database
    28,147,835 CompaniesForex
    Forex South Africa 113,550 Forex Traders

    Forex Australia 135,696 Forex Traders
    Forex UK 779,674 Forex Traders

    UK Companies Database
    521,303 CompaniesGerman Databases
    German Companies Database: 2,209,191 Companies

    German Executives Database: 985,048 Executives

    Australian Companies Database
    1,806,596 CompaniesUAE Companies Database
    950,652 CompaniesAffiliate Marketers Database
    494,909 recordsSouth African Databases
    B2B Companies Database: 1,462,227 Companies

    Directors Database: 758,834 Directors
    Healthcare Database: 376,599 Medical Professionals
    Wholesalers Database: 106,932 Wholesalers
    Real Estate Agent Database: 257,980 Estate Agents
    Forex South Africa: 113,550 Forex Traders

    Visit www.stardatagroup.com or contact us with any queries.

    Kind Regards,
    StarDataGroup.com

    Kala Godley 2021-01-18 09:04:45回复