学无先后,达者为师

网站首页 前端文档 正文

JavaScript 利用监听实现右键检查就 Debugger

作者:weixin_44953227 更新时间: 2022-04-09 前端文档

目录

  • 完整代码
  • 核心代码

完整代码

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传title>
head>
<body>
    <form action="http://localhost:8080/upload" method="POST">
        <p>
            上传人: <input type="text" name="username" />
        p>
        
        <p>
            文件1: <input type="file" name="file1" />
        p>
        
        <p>
            文件2: <input type="file" name="file2" />
        p>

        <input type="submit" /> | <input type="reset" />
    form>

    <h2>右键检查试试h2>

    <script>
        var check = (function () {
            var callbacks = [], timeLimit = 2, open = false;
            setInterval(loop, 2);
            return {
                addListener: function (fn) {
                    callbacks.push(fn);
                },
                cancleListenr: function (fn) {
                    callbacks = callbacks.filter(function (v) {
                        return v !== fn;
                    });
                }
            }
            function loop() {
                var startTime = new Date();
                debugger;

                if (new Date() - startTime > timeLimit) {
                    if (!open) {
                        callbacks.forEach(function (fn) {
                            fn.call(null);
                        });
                    }
                    open = true;
                } else {
                    open = false;
                }
            }
        })();

        check.addListener(function () {
            //alert('Open Devtool');
        });
    script>
body>
html>

核心代码

((function() {
    var callbacks = [],
        timeLimit = 50,
        open = false;
    setInterval(loop, 1);
    return {
        addListener: function(fn) {
            callbacks.push(fn);
        },
        cancleListenr: function(fn) {
            callbacks = callbacks.filter(function(v) {
                return v !== fn;
            });
        }
    }

    function loop() {
        var startTime = new Date();
        debugger;
        if (new Date() - startTime > timeLimit) {
            if (!open) {
                callbacks.forEach(function(fn) {
                    fn.call(null);
                });
            }
            open = true;
            window.stop();
            document.body.innerHTML = "";
        } else {
            open = false;
        }
    }
})()).addListener(function() {
    window.location.reload();
});

原文链接:https://blog.csdn.net/weixin_44953227/article/details/112576352

栏目分类
最近更新