禁止用户打开控制台方法汇总

禁用鼠标右键

1
2
3
window.oncontextmenu = function () {
return false;
};

限制右键菜单,防止通过右键菜单来打开控制台

限制通过键盘打开控制台

1
2
3
4
window.onkeydown = window.onkeyup = window.onkeypress = function () {
window.event.returnValue = false;
return false;
};

此方法会限制一切的键盘操作,所以可以实现无法通过一切键盘快捷方式打开控制台

打开控制台后进进行限制

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var ConsoleManager = {
onOpen: function () {
console.log("Console is opened");
},
onClose: function () {
console.log("Console is closed");
},
init: function () {
var self = this;
var x = document.createElement("div");
var isOpening = false,
isOpened = false;
Object.defineProperty(x, "id", {
get: function () {
if (!isOpening) {
self.onOpen();
isOpening = true;
}
isOpened = true;
},
});
setInterval(function () {
isOpened = false;
console.info(x);
console.clear();
if (!isOpened && isOpening) {
self.onClose();
isOpening = false;
}
}, 200);
},
};

ConsoleManager.onOpen = function () {
//打开控制台,跳转到百度
try {
window.open("about:blank", (target = "_self"));
} catch (err) {
var a = document.createElement("button");
a.onclick = function () {
window.open("about:blank", (target = "_self"));
};
a.click();
}
};
ConsoleManager.onClose = function () {
console.log("Console is closed!!!!!");
};
ConsoleManager.init();

此方法会通过定时器来时刻监听控制台是否打开,一旦打开则跳转到其他页面

1
2
3
4
5
6
7
8
var h = window.innerHeight,
w = window.innerWidth;
window.onresize = function () {
if (h != window.innerHeight || w != window.innerWidth) {
window.close();
window.location = "about:blank";
}
};

此方法会通过监听窗口尺寸大小的变化,当由于打开控制台所导致的窗口大小发生变化的时候就会跳转到其他页面

-------------本文结束感谢您的阅读-------------