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

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

禁止浏览器默认右键菜单

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

禁止浏览器文本选中

  • js
if(document.all){
    document.onselectstart= function(){return false;}; //for ie
}else{
    document.onmousedown= function(){return false;};
    document.onmouseup= function(){return true;};
}
document.onselectstart = new Function('event.returnValue=false;');
  • css

禁止复制内容

document.oncopy = function (event) {
    if (window.event) {
        event = window.event;
    }
    try {
        var the = event.srcElement;
        if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
            return false;
        }
        return true;
    } catch (e) {
        return false;
    }
}

禁止F12打开控制台

document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
    var e = event || window.event || arguments.callee.caller.arguments[0];

    if (e && e.keyCode == 123) {
            e.returnValue = false;
            return (false);
    }
}

终极武器

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

var ConsoleManager={
            onOpen:function(){
                alert("Console is opened")
            },
            onClose:function(){
                alert("Console is closed")
            },
            init:function(){
                var self = this;
                var x = document.createElement('div');
                var isOpening = false,isOpened=false;
                Object.defineProperty(x, 'id', {
                    get:function(){
                        if(!isOpening){
                            self.onOpen();
                            isOpening=true;
                        }
                        isOpened=true;
                    }
                });
                setInterval(function(){
                    isOpened=false;
                    console.info(x);
                    console.clear();
                    if(!isOpened && isOpening){
                        self.onClose();
                        isOpening=false;
                    }
                },200)
            }
        }

        ConsoleManager.onOpen = function(){
            //打开控制台,跳转到百度
            try{
                window.open('https://www.baidu.com/',target='_self');
            }catch(err){
                var a = document.createElement("button");
                    a.onclick=function(){
                    window.open('https://www.baidu.com',target='_self');
                }
                a.click();  
            }
        }
        ConsoleManager.onClose = function(){
            alert("Console is closed!!!!!")
        }
        ConsoleManager.init();

你可能感兴趣的:(禁止用户打开浏览器控制台)