简单前端web页面文字防复制 粘贴 剪切 鼠标右键 F12 shift+ctrl+i等,多浏览器支持

简单的实现javaScript层面的文字防复制,只能说防君子不妨小人,有兴趣的人可以试试

<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<style>

body {
  -moz-user-select: none; /*火狐*/
  -webkit-user-select: none; /*webkit浏览器*/
  -ms-user-select: none; /*IE10*/
  -khtml-user-select: none; /*早期浏览器*/
  -o-user-select:none;
  user-select: none;
  
}
</style>
</head>
 
<body>
    
	<div>		
	<h5>禁止复制+剪切禁止右键,右键某些选项:全选,复制,粘贴等。
		禁用文字选择,能选择却不能复制,体验很差。
		user-select 用css禁止选择文本。
		</h5>
	</div>
 
    <script>
    	    // 禁止右键菜单
    document.body.oncontextmenu = e => {
        console.log(e, '右键');
        return false;
        // e.preventDefault();
    };
    // 禁止文字选择。
    document.body.onselectstart = e => {
        console.log(e, '文字选择');
        return false;
        // e.preventDefault();
    };
    // 禁止复制
    document.body.oncopy = e => {
        console.log(e, 'copy');
        return false; 
        // e.preventDefault();
    }
    // 禁止剪切
    document.body.oncut = e => {
        console.log(e, 'cut');
        return false;
        // e.preventDefault();
    };
    // 禁止粘贴
    document.body.onpaste = e => {
        console.log(e, 'paste');
        return false;
        // e.preventDefault();
    };
	document.body.contentEditable="false"
    // css 禁止文本选择 这样不会触发js
	//禁止F12 shift+ctrl+i
    document.onkeydown = function () {
		console.log(111,window.event)
		console.log(22222,window.event.keyCode)
            if (window.event && window.event.keyCode == 123 || 
			window.event.keyCode == 17 || window.event.keyCode == 16 || window.event.keyCode == 73) {
                event.keyCode = 0;
                event.returnValue = false;
                return false;
            }
        };
	//禁止鼠标右键
		document.oncontextmenu = function(){
			event.returnValue = false;
		}//另一种
		document.oncontextmenu = function(){
			return false;
		}


    </script>
</body>
</html>

在现实开发中其实有更高级的文字防复制的方式

使用字体库进行加密,入如果有想了解的可以参考

前端反爬小技巧之字体库加密 - 知乎

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值