前言:
在网络高度发达的今天,复制和转载文章已成为一种常态。然而,这种便捷的行为却时常触碰到版权的红线,给原创作者带来不应有的损失。为了捍卫原创者的权益,众多网站开始采取积极措施,当读者尝试复制文章内容时,会触发版权提示框,以提醒用户尊重并保护原创作者的版权。
代码①:
通过 SweetAlert 美化的提示框,加载 JS 和 CSS,但会影响速度:
// 版权提示 _www.xjvae.com
function zm_copyright_tips() {
echo '<link rel="stylesheet" type="text/css" rel="external nofollow" target="_blank" href="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.css">';
echo '<script src="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.js"></script>';
echo '<script>document.body.oncopy = function() { swal("复制成功!", "转载请务必保留原文链接,申明来源,谢谢合作!!","success");};</script>';
}
add_action( 'wp_footer', 'zm_copyright_tips', 100 );
使用方法:
主题根目录 -> functions.php 末尾
实际效果①:

代码②:
//检测复制版权提示
function copyright_reminder() {
?>
<script type="text/javascript">
document.body.oncopy=function(){alert('复制成功,若要转载请务必保留原文链接,谢谢合作!');}
</script>
<?php
}
add_action('wp_footer','copyright_reminder');
//结束
使用方法:
主题根目录 -> functions.php 末尾。
实际效果②:

代码③:(弹窗 3 秒后自动消失)
<script src="https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.js">
</script>
<script type="text/javascript">document.body.oncopy = function() {
layer.msg('复制成功,若要转载请务必保留出处!');};
</script>
使用方法:
引入 JS,将代码复制添加至 / body 前面的代码里,一般在 footer.php,使用的是 layui layer/3.1.1/layer.js web 弹层组件。
也可将代码插入主题设置 -> 插入代码 -> 页头代码。
实际效果:

ps:文章复制添加来源链接
<script ="text/javascript">function addLink() {
var selection = window.getSelection();
pagelink = ". 原文出自[xjvae 博客] 转载请保留原文链接: " + document.location.href;
copytext = selection + pagelink;
newdiv = document.createElement('div');
newdiv.style.position = 'absolute';
newdiv.style.left = '-99999px';
document.body.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function () {
document.body.removeChild(newdiv);
}, 100);
}
document.oncopy = addLink;</script>
使用方法:
添加至主题文件夹的 header.php 中即可。【CoreNext/template/views】
实际效果:

代码④:(利用 element 显示 Notification 通知功能来实现)
安装 Node.js 版本管理器:进入宝塔 -->软件商店 -->Node.js 版本管理器 2.1 -->设置 -->安装 v16.9.0 -->命令行版本选择 v16.9.0

安装 element UI:进入宝塔文件 -->找到网站 -->终端 -->输入
npm install element-ui --save
出现黄色警告无视。这时刷新文件或网页会出现 node_modules 文件夹,至此 element UI 已安装完成。

添加 js 代码(在任意引用全局 js 的文件内添加代码;或者直接新建 js,以脚本方式运行)
/* 复制提醒 */
document.addEventListener("copy",function(e){
new Vue({
data:function(){
this.$notify({
title:"嘿!复制成功",
message:"若要转载请务必保留原文链接!爱你呦~",
position: 'bottom-right',
offset: 50,
showClose: false,
type:"success"
});
return{visible:false}
}
})
})
将代码插入主题设置 -> 插入代码 -> 页头代码
<!-- 引入 VUE -->
<script src="你的站点/node_modules/vue/dist/vue.min.js"></script>
<!-- 引入样式 -->
<script src="你的站点/node_modules/element-ui/lib/index.js"></script>
<!-- 引入组件库 -->
<link rel="stylesheet" href="你的站点/node_modules/element-ui/packages/theme-chalk/lib/index.css">
<!-- 运行脚本 -->
<script src="你的站点/wp-content/themes/CoreNext/static/js/vue.js"></script>
实际效果:

ps:添加使用以下代码后记得清理本地缓存
/* 禁用 F12 按键并提醒 */
document.onkeydown = function () {
if (window.event && window.event.keyCode == 123) {
event.keyCode = 0;
event.returnValue = false;
new Vue({
data:function(){
this.$notify({
title:"嘿!别瞎按",
message:"坏孩子!",
position: 'bottom-right',
offset: 50,
showClose: false,
type:"error"
});
return{visible:false}
}
})
return false;
}
};
/* 禁用右键菜单并提醒 */
document.oncontextmenu = function () {
new Vue({
data:function(){
this.$notify({
title:"嘿!没有右键菜单",
message:"复制请用键盘快捷键",
position: 'bottom-right',
offset: 50,
showClose: false,
type:"warning"
});
return{visible:false}
}
})
return false;
}
//禁用左键选择
document.onselectstart = function () {
return false;
}
//禁用复制
document.oncopy = function () {
return false;
}
//禁用 Ctrl+Shift+I
if ((event.ctrlKey) && (event.shiftKey) && (event.keyCode == 73)) {
return false;
}
来源参考:
好软猫:https://www.haoruanmao.com
你的明明呐丶:https://cloud.tencent.com/developer/article/2031306