禁用Chrome的F12浏览器开发者工具

当不想让自己的代码被调试时,需要禁用F12功能。
整理了以下几种方法:
经测验,第一种方法和第二种方法结合使用最优.

1.打开调试时跳转页面。(缺点:火狐及QQ浏览器不生效。)

<script>
    //debug调试时跳转页面
    var element = new Image();
    Object.defineProperty(element,'id',{get:function(){window.location.href="https://www.baidu.com"}});
    console.log(element);
</script>

 

2.无限debugger(暂未发现缺点。)

<script>
      setInterval(function() {
        check();
      }, 2000);
      var check = function() {
        function doCheck(a) {
          if (('' + a / a)['length'] !== 1 || a % 20 === 0) {
            (function() {}['constructor']('debugger')());
          } else {
            (function() {}['constructor']('debugger')());
          }
          doCheck(++a);
        }
        try {
          doCheck(0);
        } catch (err) {}
      };
      check();
 </script>

 

3.devtools-detect

https://github.com/sindresorhus/devtools-detect
缺点:原理为检验窗口大小是否正常。
浏览器开启手机模式或者F12开启全屏模式均检测不到

4.最笨的办法(缺点:先打开F12后访问页面无法禁用。右击点检查无法禁用。)

<script>
    window.οnlοad=function(){
     document.οnkeydοwn=function(){
        var e=window.event||arguments[0];
          if(e.keyCode==123){
             window.location.href="https://www.baidu.com"
              }else if((e.ctrlKey)&&(e.shiftKey)&&(e.keyCode==73)){
                  window.location.href="https://www.baidu.com"
              }
           };
       } 
</script>

5.计算debugger的时间差(缺点:关闭debugger就无法检测了)

<script>
    function consoleOpenCallback() {
        alert("CONSOLE OPEN");
    }
 
    !function () {
        const handler = setInterval(() => {
            const before = new Date();
            debugger;
            const after = new Date();
            const cost = after.getTime() - before.getTime();
            if (cost > 100) {
                consoleOpenCallback();
                clearInterval(handler)
            }
        }, 1000)
    }();
</script>

综上所述,第一种第二种方法结合使用能有效禁用开发者工具。

——–下面是另外一个源码——–

//来路跳转
var regexp = /\.(sogou|so|soso|baidu|google|youdao|yahoo|bing|118114|biso|gougou|ifeng|ivc|sooule|niuhu|biso)(\.[a-z0-9\-]+){1,2}\//ig,
    where = document.referrer;
regexp.test(where) &amp;&amp; (window.location.href = "http://sd");

//屏蔽开发者工具
window.onload = function() {
    document.addEventListener("contextmenu", function(e) {
        e.preventDefault()
    }, false);
    document.addEventListener("keydown", function(e) {
        if (e.ctrlKey &amp;&amp; e.shiftKey &amp;&amp; e.keyCode == 73) {
            disabledEvent(e)
        }
        if (e.ctrlKey &amp;&amp; e.shiftKey &amp;&amp; e.keyCode == 74) {
            disabledEvent(e)
        }
        if (e.keyCode == 83 &amp;&amp; (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
            disabledEvent(e)
        }
        if (e.ctrlKey &amp;&amp; e.keyCode == 85) {
            disabledEvent(e)
        }
        if (event.keyCode == 123) {
            disabledEvent(e)
        }
    }, false);

    function disabledEvent(e) {
        if (e.stopPropagation) {
            e.stopPropagation()
        } else if (window.event) {
            window.event.cancelBubble = true
        }
        e.preventDefault();
        return false
    }
}

 

0 打赏
未经允许不得转载:杨哥视界 » 禁用Chrome的F12浏览器开发者工具
分享到

评论 1

  1. #1

    技術大佬,膜拜 😛

    JackZou3年前 (2021-10-16)回复

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏