禁止用戶打開瀏覽器控制臺(tái)
禁止瀏覽器默認(rèn)右鍵菜單
document.oncontextmenu = function (event) {
event.preventDefault();
};
禁止瀏覽器文本選中
- js
if(document.all){
document.onselectstart= function(){return false;}; //for ie
}else{
document.onmousedown= function(){return false;};
document.onmouseup= function(){return true;};
}
document.onselectstart = new Function('event.returnValue=false;');
- css
<style type="text/css">
body {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
-khtml-user-select: none;
user-select: none;
}
</style>
禁止復(fù)制內(nèi)容
document.oncopy = function (event) {
if (window.event) {
event = window.event;
}
try {
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
return false;
}
return true;
} catch (e) {
return false;
}
}
禁止F12打開控制臺(tái)
document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
var e = event || window.event || arguments.callee.caller.arguments[0];
if (e && e.keyCode == 123) {
e.returnValue = false;
return (false);
}
}
終極武器
上述方法禁止右鍵和F12打開控制臺(tái)甩恼,但是知道打開控制臺(tái)的人基本上都是懂技術(shù)的惠毁,這點(diǎn)限制還難不倒他們,他們可以通過Ctrl+Shift+I或者瀏覽器設(shè)置進(jìn)入開發(fā)者模式荐吉,實(shí)乃帝國主義亡我之心不死,對(duì)付這種狡詐惡徒稠鼻,需要祭上最終手段捺信。我們檢測(cè)用戶是否打開控制臺(tái),如果打開仁烹,我們對(duì)網(wǎng)頁進(jìn)行一些操作耸弄,例如:強(qiáng)制跳轉(zhuǎn)頁面。
var ConsoleManager={
onOpen:function(){
alert("Console is opened")
},
onClose:function(){
alert("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(){
//打開控制臺(tái)晃危,跳轉(zhuǎn)到百度
try{
window.open('https://www.baidu.com/',target='_self');
}catch(err){
var a = document.createElement("button");
a.onclick=function(){
window.open('https://www.baidu.com',target='_self');
}
a.click();
}
}
ConsoleManager.onClose = function(){
alert("Console is closed!!!!!")
}
ConsoleManager.init();