现在的社会越来越注重版权了,在百度搜索图片时,有版权的图片都会显示“版权”二字。有时候我们辛辛苦苦敲出来的文字,别人不打个招呼就拿走了,实在不厚道。所以写了下面这段提醒代码,可以自动跟在所复制文字的后面,虽然还是可以删掉,但是提醒一下总归有好处吧。
一般情况下,你百度出来的信息是这样的:打开wordpress模版里面的functions.php文件,把下面的代码放到最后面即可:
//复制文字自动添加版权信息 function nc_copyright() { ?> <script type='text/javascript'> function addLink() { var body_element = document.getElementsByTagName('body')[0]; var selection; selection = window.getSelection(); var pagelink = "<br /><br /> 本文来源于:<?php bloginfo('name'); ?><br />原文标题: <?php if(is_single()){ the_title();}?><br /> 原文链接:<a href='"+document.location.href+"'>"+document.location.href+"</a>"; var copy_text = selection + pagelink; var new_div = document.createElement('div'); new_div.style.left='-99999px'; new_div.style.position='absolute'; body_element.appendChild(new_div ); new_div.innerHTML = copy_text ; selection.selectAllChildren(new_div ); window.setTimeout(function() { body_element.removeChild(new_div ); },0); } document.oncopy = addLink; </script> <?php } add_action( 'wp_head', 'nc_copyright');
原理就是,新建一个div,把选择的内容放到这个新建的div里的时候加上了版权信息,然后选中里面的内容。这一系列操作是你进行复制的时候激活的。不过这段代码有个弊端,就是复制的内容有JS语句时不生效。原因就是js代码只能在代码模式下看到,所以没法选择。我用textarea解决了这个问题,代码如下:
//复制文字自动添加版权信息 function nc_copyright() { ?> <script type='text/javascript'> function addLink() { var body_element = document.getElementsByTagName('body')[0]; var selection; selection = window.getSelection(); var pagelink = " 本文来源于:<?php bloginfo('name'); ?> 原文标题: <?php if(is_single()){ the_title();}?> 原文链接:<a href='"+document.location.href+"'>"+document.location.href+"</a>"; var copy_text = selection + pagelink; var new_div = document.createElement('textarea'); new_div.style.left='-99999px'; new_div.style.position='absolute'; body_element.appendChild(new_div ); new_div.value = copy_text; new_div.select(); window.setTimeout(function() { body_element.removeChild(new_div ); },0); } document.oncopy = addLink; </script> <?php } add_action( 'wp_head', 'nc_copyright');
原理都是一样的,下面的代码也可以实现,记得在前面引入jQuery:
<script> $("body").bind('copy', function (e) { if (typeof window.getSelection == "undefined") return; //IE8 及更老的版本不兼容 var body_element = document.getElementsByTagName('body')[0]; var selection = window.getSelection(); //如果选择是短的内容,让我们不要打扰我们的用户 if (("" + selection).length < 30) return; //创建一个DIV的可见区域之外 //并填写选定的文本 var newdiv = document.createElement('div'); newdiv.style.position = 'absolute'; newdiv.style.left = '-99999px'; body_element.appendChild(newdiv); newdiv.appendChild(selection.getRangeAt(0).cloneContents()); //我们需要<pre>标签解决方案 //其他的文本在<pre>失去了所有的行符! if (selection.getRangeAt(0).commonAncestorContainer.nodeName == "PRE") { newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>"; } newdiv.innerHTML += "<br /><br />文章来自: 网站名称() 详文参考:<a href='" + document.location.href + "'>" + document.location.href + "</a>"; selection.selectAllChildren(newdiv); window.setTimeout(function () { body_element.removeChild(newdiv); }, 200); }); </script>
有问题可在下方评论留言,或关注“大超小志”微信公众号留言。
标签: wordpress javascript 版权
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
留言评论
如需留言或评论,请在微信中打开此页面。