JS禁用页面选中,复制,F12按键,右键菜单等功能

今天在看博客园,有个很有意思的事情,博客园推荐的文章,我随便进来一个页面。

这个页面有右键屏蔽,复制,F12按键等功能,随后打开页面源码看了一下,有如下代码:

<script>
    //禁用F12
    window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
        // 判断是否按下F12,F12键码为123
        if (event.keyCode == 123) {
            event.preventDefault(); // 阻止默认事件行为
            window.event.returnValue = false;
        }
    }
    //禁用调试工具
    var threshold = 160; // 打开控制台的宽或高阈值
    // 每秒检查一次
    var check = setInterval(function() {
        if (window.outerWidth - window.innerWidth > threshold ||
            window.outerHeight - window.innerHeight > threshold) {
            // 如果打开控制台,则刷新页面
            // window.location.reload();
        }
    }, 1000)
    //屏蔽右键菜单
    document.oncontextmenu = 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;
        }
    }
    //屏蔽选中
    document.onselectstart = 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;
        }
    }
    //屏蔽复制
    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;
        }
    }
    //屏蔽剪贴
    document.oncut = 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;
        }
    }
    //屏蔽粘贴
    document.onpaste = 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;
        }
    }
    window.onload=function(){
        document.onkeydown=function(){
            var e = window.event||arguments[0];
            if(e.keyCode==123){
                //F12
                showMsg();
                return false;
            }else if((e.ctrlKey)&&(e.shiftKey)&&(e.keyCode==73)){
                //ctrl + shift + i
                showMsg();
                return false;
            }else if((e.ctrlKey)&&(e.keyCode==85)){
                //ctrl + U
                showMsg();
                return false;
            }else if((e.ctrlKey)&&(e.keyCode==83)){
                //ctrl + U
                showMsg();
                return false;
            }
        }
        document.oncontextmenu=function(){
            //右击
            showMsg();
            return false;
        }
    }

    function showMsg() {
        $(function () {
            //弹窗调用接口
            function tc(txt) {
                var html = "<div class=\"box_fixed\"><div class=\"box_fixed_box\"><h4>温馨提示</h4><span></span><div>我知道了</div></div></div>";
                $("body").append(html);
                $(".box_fixed_box span").text(txt);
                $(".box_fixed").show();
                $(".box_fixed_box div").click(function () {
                    $(".box_fixed").remove();
                });
            };
            tc("很抱歉,CV大法被禁止!为保护作者权益,本博客文章不允许复制。\n\n如有特殊需要,请添加作者微信");
        });

}
</script>

这个技术对大多数人有效,稍微懂些前端的无效。

有需要的可以拿去。

 

 

2025-07-15 10:46:47【参考文章出处链接】:https://www.cnblogs.com/longronglang/p/18984790

=======================================================================================

posted on 2025-07-15 11:03  jack_Meng  阅读(56)  评论(0)    收藏  举报

导航