摘取
禁止瀏覽器默認右鍵菜單
document.oncontextmenu = function (event) {
event.preventDefault();
}
禁止瀏覽器文本選中
- JavaScript
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>
禁止復制內容
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打開控制臺
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打開控制臺,但是知道打開控制臺的人基本上都是懂技術的,這點限制還難不倒他們乾戏,他們可以通過Ctrl+Shift+I或者瀏覽器設置進入開發(fā)者模式模暗,實乃帝國主義亡我之心不死岸晦,對付這種狡詐惡徒欧啤,需要祭上最終手段。我們檢測用戶是否打開控制臺启上,如果打開邢隧,我們對網(wǎng)頁進行一些操作,例如:強制跳轉頁面冈在。
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(){
//打開控制臺倒慧,跳轉到百度
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();
作者:桐間紗路
鏈接:http://www.reibang.com/p/1c171cb86dbb