限制浏览器调试

2021/4/29 JavaScript

限制右键

function oncontextmenu(event) {
    console.log(event)
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if (e.button == 2) {
        return false;
    }
}
1
2
3
4
5
6
7

限制按键按下

function onkeydown(e) {
    var currKey = 0
    var evt = e || window.event;
    currKey = evt.keyCode || evt.which || evt.charCode;
    if (e.shiftKey && (currKey === 73 || currKey === 74)) {
        return false
    } else if (currKey == 123) {
        return false
    }
}
1
2
3
4
5
6
7
8
9
10

通过比对页面和浏览器的宽高来动态处理body中的内容

// 请将脚本放置到body末尾使用,保证控制台被无故开启后脚本泄漏的风险
var isOpenConsole = false
var current = window.location.href;
(function () {
    // 是否有解除限制的标识,解除请在href后增加'?nolimit'
    if (current.indexOf('nolimit') === -1) {
        console.info('启用浏览器调试限制:\n 1.限制右键\n 2.限制F12\n 3.限制保存图片\n 4.限制复制\n 5.限制选择')
        // 限制: 右键
        document.oncontextmenu = oncontextmenu;
        // 限制: f12
        document.onkeydown = onkeydown;
        // 限制: 浏览器通过设置开启开发模式
        isConsole();
        window.onresize = isConsole;
        // 限制: 保存,拖拽图片
        for (i in document.images) {
            document.images[i].ondragstart = function () { return false; };
        }
        // 限制: 复制
        document.body.oncopy = function () {
            return false;
        }
        // 限制: 选择
        document.onselectstart = function () {
            return false;
        }
        // 恢复: 已经开启控制台后已经将内容修改,监听关闭控制台的快捷键被抬起后刷新页面
        document.onkeyup = onkeyup
    }
})()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

恢复后刷新页面

function onkeyup(e) {
    var currKey = 0
    var evt = e || window.event;
    currKey = evt.keyCode || evt.which || evt.charCode;
    if (e.shiftKey && (currKey === 73 || currKey === 74)) {
        location.reload()
        return false
    } else if (currKey == 123) {
        location.reload()
        return false
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

添加移出限制的控制

// 请将脚本放置到body末尾使用,保证控制台被无故开启后脚本泄漏的风险
var isOpenConsole = false
var current = window.location.href;
(function () {
    // 是否有解除限制的标识,解除请在href后增加'?remove-limit'
    if (current.indexOf('remove-limit') === -1) {
        console.info('启用浏览器调试限制:\n 1.限制右键\n 2.限制F12\n 3.限制保存图片\n 4.限制复制\n 5.限制选择')
        // 限制: 右键
        document.oncontextmenu = oncontextmenu;
        // 限制: f12
        document.onkeydown = onkeydown;
        // 限制: 浏览器通过设置开启开发模式
        isConsole();
        window.onresize = isConsole;
        // 限制: 保存,拖拽图片
        for (i in document.images) {
            document.images[i].ondragstart = function () { return false; };
        }
        // 限制: 复制
        document.body.oncopy = function () {
            return false;
        }
        // 限制: 选择
        document.onselectstart = function () {
            return false;
        }
        // 恢复: 已经开启控制台后已经将内容修改,监听关闭控制台的快捷键被抬起后刷新页面
        document.onkeyup = onkeyup
    }
})()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Last Updated: 2023/2/1 03:59:51