[反调试]审查元素清空页面或重定向

web前端 67 3周前 (08-21)

为防止用户通过浏览器审查元素代码,以保护网页内容的安全性和隐私性。以下是一个反调试示例代码,可以阻止用户通过审查元素查看网页源代码,变量为1时清空页面内容,变量为2时重定向到指定页面。

反调试脚本

function checkDevTools() {
    const threshold = 160;
    const devtoolsOpen = window.outerWidth - window.innerWidth > threshold || window.outerHeight - window.innerHeight > threshold;
    if (devtoolsOpen) {
        /*
        *   debugprotect =
        *   1: 审查元素时->[清空页面内容]
        *   2: 审查元素时->[重定向到指定页面]
        */
        const debugprotect = '1';
        switch (debugprotect) {
            case '1':
                document.documentElement.parentNode.replaceChild(document.createElement('html'), document.documentElement);
                break;
            case '2':
                window.location.href = 'https://www.toyean.com/';
                break;
        }
    }
}
setInterval(checkDevTools, 100);

完整代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>[反调试]审查元素清空页面或重定向-拓源网</title>
    <script>
        function checkDevTools() {
            const threshold = 160;
            const devtoolsOpen = window.outerWidth - window.innerWidth > threshold || window.outerHeight - window.innerHeight > threshold;
            if (devtoolsOpen) {
                /*
                *   debugprotect =
                *   1: 审查元素时->[清空页面内容]
                *   2: 审查元素时->[重定向到指定页面]
                */
                const debugprotect = '1';
                switch (debugprotect) {
                    case '1':
                        document.documentElement.parentNode.replaceChild(document.createElement('html'), document.documentElement);
                        break;
                    case '2':
                        window.location.href = 'https://www.toyean.com/';
                        break;
                }
            }
        }
        setInterval(checkDevTools, 100);
    </script>
</head>
<body>
    <p>热爱可抵 岁月漫长!</p>
</body>
</html>

猛戳查看->演示效果

需要注意的是,反调试方法并非绝对可靠,一些高级用户仍可能绕过限制。

以下是绕过限制的方法介绍,和升级后的反调试代码,增加了performance检测、禁用鼠标右键、禁用选中文本、禁用复制文本、无限debugger、以及禁用调试快捷键等:

呜呼~~ 您需要有拓源网 VIP会员 账号,并且 登录 后即可查看或下载隐藏部分的内容.

分享给朋友: