HTML网页禁止右键查看源代码

<script>
        // 禁止右键菜单
        document.addEventListener('contextmenu', function(e) {
            e.preventDefault();
            showAlert('右键菜单已被禁用');
        });

        // 禁止F12开发者工具
        document.addEventListener('keydown', function(e) {
            // F12
            if (e.key === 'F12') {
                e.preventDefault();
                showAlert('F12开发者工具已被禁用');
            }
            // Ctrl+U
            if (e.ctrlKey && e.key === 'u') {
                e.preventDefault();
                showAlert('Ctrl+U查看源代码已被禁用');
            }
            // Ctrl+S
            if (e.ctrlKey && e.key === 's') {
                e.preventDefault();
                showAlert('Ctrl+S保存网页已被禁用');
            }
            // Ctrl+Shift+I
            if (e.ctrlKey && e.shiftKey && e.key === 'i') {
                e.preventDefault();
                showAlert('Ctrl+Shift+I开发者工具已被禁用');
            }
            // Ctrl+Shift+C
            if (e.ctrlKey && e.shiftKey && e.key === 'c') {
                e.preventDefault();
                showAlert('Ctrl+Shift+C元素选择器已被禁用');
            }
        });

        // 监测开发者工具是否打开(通过监测窗口尺寸变化)
        let devtoolsOpen = false;
        let lastInnerWidth = window.innerWidth;
        let lastInnerHeight = window.innerHeight;

        setInterval(function() {
            if (
                (window.innerWidth !== lastInnerWidth && Math.abs(window.innerWidth - lastInnerWidth) > 100) ||
                (window.innerHeight !== lastInnerHeight && Math.abs(window.innerHeight - lastInnerHeight) > 100)
            ) {
                devtoolsOpen = true;
                showAlert('检测到开发者工具已打开');
                document.body.innerHTML = '<div class="fixed inset-0 bg-dark flex items-center justify-center text-white text-xl font-bold">检测到开发者工具已打开,请关闭后继续访问。</div>';
            }
            lastInnerWidth = window.innerWidth;
            lastInnerHeight = window.innerHeight;
        }, 1000);

        // 测试按钮功能
        document.getElementById('testButton').addEventListener('click', function() {
            document.getElementById('testResult').classList.remove('hidden');
            setTimeout(() => {
                document.getElementById('testResult').classList.add('hidden');
            }, 3000);
        });

        // 显示警告提示
        function showAlert(message) {
            // 创建提示元素
            const alertBox = document.createElement('div');
            alertBox.className = 'fixed top-4 right-4 bg-red-500 text-white px-4 py-2 rounded-md shadow-lg z-50 transform transition-all duration-300 opacity-0 translate-y-[-20px]';
            alertBox.textContent = message;
            
            // 添加到页面
            document.body.appendChild(alertBox);
            
            // 显示动画
            setTimeout(() => {
                alertBox.classList.remove('opacity-0', 'translate-y-[-20px]');
            }, 10);
            
            // 3秒后隐藏
            setTimeout(() => {
                alertBox.classList.add('opacity-0', 'translate-y-[-20px]');
                setTimeout(() => {
                    document.body.removeChild(alertBox);
                }, 300);
            }, 3000);
        }
    </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄大新

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值