JS代码实现复制内容自动添加原文版权信息

  • A+
所属分类:源码教程

将以下JS代码放到主题页脚文件里

// WordPress复制内容自动添加版权信息
<script type="text/javascript">
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
if (window.ActiveXObject) {
    document.body.oncopy = function() {
        event.returnValue = false;
        var t = document.selection.createRange().text;
        var s = "\r\n原文出自[ 墨轩博客 ] 转载请保留原文链接:" + location.href;
        clipboardData.setData('Text', t + '\r\n' + s);
    }
} else {
    function addLink() {
        var body_element = document.getElementsByTagName('body')[0];
        var selection;
        selection = window.getSelection();
        var pagelink = "原文出自[ 墨轩博客 ] 转载请保留原文链接:" + document.location.href;
        var copytext = selection + pagelink;
        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 = addLink;
}
</script>

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: