墨轩
[资讯杂谈]非插件实现WordPress文章防复制和禁止右键菜单
使用赵墨轩部落格提供的WordPress非插件文章防复制JS代码,可以有效地防止别人直接复制拷贝你的文章,用frame
标签引用你的文章时,会自动跳转到文章正常链接,同时禁止右键菜单。但是以下代码仅供参考,毕竟从技术层面来说,只防君子,不防小人!从体验来说,体验不是很好,毕竟互联网本来就是分享的价值观!
代码一
打开当前主题头部模板header.php
找到:<?php wp_head(); ?>
将下面代码添加到后面:
<script>
// 禁止右键
document.oncontextmenu = function() {
return false
};
// 禁止图片拖放
document.ondragstart = function() {
return false
};
// 禁止选择文本
document.onselectstart = function() {
if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false;
else return true;
};
if (window.sidebar) {
document.onmousedown = function(e) {
var obj = e.target;
if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true;
else return false;
}
};
// 禁止frame标签引用
if (parent.frames.length > 0) top.location.replace(document.location);
</script>
代码二
// 禁止右键
document.oncontextmenu = function() {
return false
};
// 禁止图片拖放
document.ondragstart = function() {
return false
};
// 禁止选择文本
document.onselectstart = function() {
if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false;
else return true;
};
if (window.sidebar) {
document.onmousedown = function(e) {
var obj = e.target;
if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true;
else return false;
}
};
// 禁止frame标签引用
if (parent.frames.length > 0) top.location.replace(document.location);
然后再将下面代码添加到当前主题函数模板functions.php
的最后:
function copyrightpro_scripts() {
wp_enqueue_script( 'copyright', get_template_directory_uri() . '/copyright.js', array(), false );
}
if (! current_user_can('level_10') ) {
add_action( 'wp_enqueue_scripts', 'copyrightpro_scripts' );
}
[scode type="yellow"]代码中加了判断,管理员登录状态一下,防复制代码无效。[/scode]
当然上面的方法,也只是忽悠一下小白,浏览器禁用JavaScript
后,将失去效果。墨轩不建议大家使用JS防复制,很多站点是技术文章,代码都复制不了,那还看什么?除非你的文章价值千金咯。而且真相采集,方法多得是。所以仅供参考学习,不建议使用!
版权属于:赵墨轩部落格
本文链接:https://www.zmxcn.cn/949.html
本文章采用「知识共享署名-相同方式共享 4.0 国际许可协议」进行许可。转载时须注明出处及本声明