使用JS禁止页面右击-禁止F12打开控制台

使用JS禁止页面右击-禁止F12打开控制台插图

有时候我们自己写的一些东西,不想让别人轻易复制抄袭,以及不想别人按F12打开控制台,看自己网站的代码信息等。那就可以使用站长下面代码提供的js代码,对用户的行为右键,和F12进行禁止,这个别人就复制不了你的内容了。

经过实验,以下方法适用于谷歌浏览器、火狐浏览器,以及使用谷歌内核的浏览器(如QQ浏览器、搜狗浏览器等),IE忘了是否支持,但是感觉是目前比较齐全的了。

使用方法:将js代码复制后,放到博客网站需要禁止打开右键,选择复制的页面末尾处即可。

<script type="text/javascript">
// 禁止鼠标右击
document.oncontextmenu = function() {
event.returnValue = false;
};
//禁用开发者工具F12
document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
let e = event || window.event || arguments.callee.caller.arguments[0];
if (e && e.keyCode == 123) {
e.returnValue = false;
return false;
}
};
let userAgent = navigator.userAgent;
if (userAgent.indexOf("Firefox") > -1) {
let checkStatus;
let devtools = /./;
devtools.toString = function() {
checkStatus = "on";
};
setInterval(function() {
checkStatus = "off";
console.log(devtools);
console.log(checkStatus);
console.clear();
if (checkStatus === "on") {
let target = "";
try {
window.open("about:blank", (target = "_self"));
} catch (err) {
let a = document.createElement("button");
a.onclick = function() {
window.open("about:blank", (target = "_self"));
};
a.click();
}
}
}, 200);
} else {
//禁用控制台
let ConsoleManager = {
onOpen: function() {
alert("Console is opened");
},
onClose: function() {
alert("Console is closed");
},
init: function() {
let self = this;
let x = document.createElement("div");
let isOpening = false,
isOpened = false;
Object.defineProperty(x, "id", {
get: function() {
if (!isOpening) {
self.onOpen();
isOpening = true;
}
isOpened = true;
return true;
}
});
setInterval(function() {
isOpened = false;
console.info(x);
console.clear();
if (!isOpened && isOpening) {
self.onClose();
isOpening = false;
}
}, 200);
}
};
ConsoleManager.onOpen = function() {
//打开控制台,跳转
let target = "";
try {
window.open("about:blank", (target = "_self"));
} catch (err) {
let a = document.createElement("button");
a.onclick = function() {
window.open("about:blank", (target = "_self"));
};
a.click();
}
};
ConsoleManager.onClose = function() {
alert("Console is closed!!!!!");
};
ConsoleManager.init();
}
</script>
<script type="text/javascript">
// 禁止鼠标右击
  document.oncontextmenu = function() {
    event.returnValue = false;
  };
  //禁用开发者工具F12
  document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
    let e = event || window.event || arguments.callee.caller.arguments[0];
    if (e && e.keyCode == 123) {
      e.returnValue = false;
      return false;
    }
  };
  let userAgent = navigator.userAgent;
  if (userAgent.indexOf("Firefox") > -1) {
    let checkStatus;
    let devtools = /./;
    devtools.toString = function() {
      checkStatus = "on";
    };
    setInterval(function() {
      checkStatus = "off";
      console.log(devtools);
      console.log(checkStatus);
      console.clear();
      if (checkStatus === "on") {
        let target = "";
        try {
          window.open("about:blank", (target = "_self"));
        } catch (err) {
          let a = document.createElement("button");
          a.onclick = function() {
            window.open("about:blank", (target = "_self"));
          };
          a.click();
        }
      }
    }, 200);
  } else {
    //禁用控制台
    let ConsoleManager = {
      onOpen: function() {
        alert("Console is opened");
      },
      onClose: function() {
        alert("Console is closed");
      },
      init: function() {
        let self = this;
        let x = document.createElement("div");
        let isOpening = false,
          isOpened = false;
        Object.defineProperty(x, "id", {
          get: function() {
            if (!isOpening) {
              self.onOpen();
              isOpening = true;
            }
            isOpened = true;
            return true;
          }
        });
        setInterval(function() {
          isOpened = false;
          console.info(x);
          console.clear();
          if (!isOpened && isOpening) {
            self.onClose();
            isOpening = false;
          }
        }, 200);
      }
    };
    ConsoleManager.onOpen = function() {
      //打开控制台,跳转
      let target = "";
      try {
        window.open("about:blank", (target = "_self"));
      } catch (err) {
        let a = document.createElement("button");
        a.onclick = function() {
          window.open("about:blank", (target = "_self"));
        };
        a.click();
      }
    };
    ConsoleManager.onClose = function() {
      alert("Console is closed!!!!!");
    };
    ConsoleManager.init();
  }
    </script>
<script type="text/javascript"> // 禁止鼠标右击 document.oncontextmenu = function() { event.returnValue = false; }; //禁用开发者工具F12 document.onkeydown = document.onkeyup = document.onkeypress = function(event) { let e = event || window.event || arguments.callee.caller.arguments[0]; if (e && e.keyCode == 123) { e.returnValue = false; return false; } }; let userAgent = navigator.userAgent; if (userAgent.indexOf("Firefox") > -1) { let checkStatus; let devtools = /./; devtools.toString = function() { checkStatus = "on"; }; setInterval(function() { checkStatus = "off"; console.log(devtools); console.log(checkStatus); console.clear(); if (checkStatus === "on") { let target = ""; try { window.open("about:blank", (target = "_self")); } catch (err) { let a = document.createElement("button"); a.onclick = function() { window.open("about:blank", (target = "_self")); }; a.click(); } } }, 200); } else { //禁用控制台 let ConsoleManager = { onOpen: function() { alert("Console is opened"); }, onClose: function() { alert("Console is closed"); }, init: function() { let self = this; let x = document.createElement("div"); let isOpening = false, isOpened = false; Object.defineProperty(x, "id", { get: function() { if (!isOpening) { self.onOpen(); isOpening = true; } isOpened = true; return true; } }); setInterval(function() { isOpened = false; console.info(x); console.clear(); if (!isOpened && isOpening) { self.onClose(); isOpening = false; } }, 200); } }; ConsoleManager.onOpen = function() { //打开控制台,跳转 let target = ""; try { window.open("about:blank", (target = "_self")); } catch (err) { let a = document.createElement("button"); a.onclick = function() { window.open("about:blank", (target = "_self")); }; a.click(); } }; ConsoleManager.onClose = function() { alert("Console is closed!!!!!"); }; ConsoleManager.init(); } </script>

更多网页防扒技术合集可以参考如下文章:

© 版权声明
THE END
喜欢点个赞支持一下吧
点赞0赏币 分享
Misery can be caused by someone being just weak and indecisive.
一个人仅仅因为软弱无能或优柔寡断就完全可能招致痛苦
评论交流 抢沙发

请登录后发表评论

    暂无评论内容