禁止用户打开浏览器控制台

禁止用户打开浏览器控制台

禁止浏览器默认右键菜单

1 document.oncontextmenu = function (event) {
2     event.preventDefault();
3 };

 

禁止浏览器文本选中

  • js
  • 1 if(document.all){
    2     document.onselectstart= function(){return false;}; //for ie
    3 }else{
    4     document.onmousedown= function(){return false;};
    5     document.onmouseup= function(){return true;};
    6 }
    7 document.onselectstart = new Function('event.returnValue=false;');
  • css
1 <style type="text/css">
2     body {
3         -moz-user-select: none;
4         -webkit-user-select: none;
5         -ms-user-select: none;
6         -khtml-user-select: none;
7         user-select: none;
8     } 
9 </style>

 

禁止复制内容

 1 document.oncopy = function (event) {
 2     if (window.event) {
 3         event = window.event;
 4     }
 5     try {
 6         var the = event.srcElement;
 7         if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
 8             return false;
 9         }
10         return true;
11     } catch (e) {
12         return false;
13     }
14 }

 

禁止F12打开控制台

1 document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
2     var e = event || window.event || arguments.callee.caller.arguments[0];
3 
4     if (e && e.keyCode == 123) {
5             e.returnValue = false;
6             return (false);
7     }
8 }

 

终极武器

上述方法禁止右键和F12打开控制台,但是知道打开控制台的人基本上都是懂技术的,这点限制还难不倒他们,他们可以通过Ctrl+Shift+I或者浏览器设置进入开发者模式,实乃帝国主义亡我之心不死,对付这种狡诈恶徒,需要祭上最终手段。我们检测用户是否打开控制台,如果打开,我们对网页进行一些操作,例如:强制跳转页面。

 1     var ConsoleManager={
 2             onOpen:function(){
 3                 alert("Console is opened")
 4             },
 5             onClose:function(){
 6                 alert("Console is closed")
 7             },
 8             init:function(){
 9                 var self = this;
10                 var x = document.createElement('div');
11                 var isOpening = false,isOpened=false;
12                 Object.defineProperty(x, 'id', {
13                     get:function(){
14                         if(!isOpening){
15                             self.onOpen();
16                             isOpening=true;
17                         }
18                         isOpened=true;
19                     }
20                 });
21                 setInterval(function(){
22                     isOpened=false;
23                     console.info(x);
24                     console.clear();
25                     if(!isOpened && isOpening){
26                         self.onClose();
27                         isOpening=false;
28                     }
29                 },200)
30             }
31         }
32 
33         ConsoleManager.onOpen = function(){
34             //打开控制台,跳转到百度
35             try{
36                 window.open('https://www.baidu.com/',target='_self');
37             }catch(err){
38                 var a = document.createElement("button");
39                     a.onclick=function(){
40                     window.open('https://www.baidu.com',target='_self');
41                 }
42                 a.click();  
43             }
44         }
45         ConsoleManager.onClose = function(){
46             alert("Console is closed!!!!!")
47         }
48         ConsoleManager.init();

作者:桐间纱路

链接:https://www.jianshu.com/p/1c171cb86dbb

posted @ 2019-10-30 16:45  安莫寒  阅读(4359)  评论(0编辑  收藏  举报