1. 禁用F12- <script>
- document.onkeydown = function (e) {
-
- var currKey = 0, evt = e || window.event;
-
- currKey = evt.keyCode || evt.which || evt.charCode;
-
- if (currKey == 123) {
-
- window.event.cancelBubble = true;
-
- window.event.returnValue = false;
-
- }
-
- }
- </script>
复制代码 2.禁用鼠标右键- <script>
- document.oncontextmenu = 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;
-
- }
-
- }
- </script>
复制代码 3.禁止复制- <script>
- 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;
- }
- }
- </script>
复制代码 4.禁止剪切- <script>
- document.oncut = 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;
- }
- }
- </script>
复制代码 禁止选中- <script>
- document.onselectstart = 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;
- }
- }
- </script>
复制代码 有效禁止F12和右击(单击浏览器设置里 开发者-审查元素 无法禁止)- <script>
- //屏蔽F12和右键
- function click(e) {
- if (document.all) {
- if (event.button == 2 || event.button == 3) {
- alert("欢迎光临寒舍,有什么需要帮忙的话,请与站长联系!谢谢您的合作!!!");
- oncontextmenu = 'return false';
- }
- }
- if (document.layers) {
- if (e.which == 3) {
- oncontextmenu = 'return false';
- }
- }
- }
- if (document.layers) {
- document.captureEvents(Event.MOUSEDOWN);
- }
- document.onmousedown = click;
- document.oncontextmenu = new Function("return false;")
- document.onkeydown = document.onkeyup = document.onkeypress = function () {
- if (window.event.keyCode == 123) {
- window.event.returnValue = false;
- return (false);
- }
- }
复制代码 有效禁止F12和右击、审查元素----设置中强行打开开发者会直接关闭网页。
该方法在demo阶段发现两个问题:
1,方法会导致手机端无法滑动,滑动或者操作会被判定为改变分辨率造成强制关闭网页的情况。
2,在别的页面打开F12在加载自己的网页时,虽然也及时关闭,但在响应里还是可以看到流量包。- <script>
- //禁用右键(防止右键查看源代码)
- window.oncontextmenu = function () { return false; }
- //禁止任何键盘敲击事件(防止F12和shift+ctrl+i调起开发者工具)
- window.onkeydown = window.onkeyup = window.onkeypress = function () {
- window.event.returnValue = false;
- return false;
- }
- //如果用户在工具栏调起开发者工具,那么判断浏览器的可视高度和可视宽度是否有改变,如有改变则关闭本页面
- var h = window.innerHeight, w = window.innerWidth;
- window.onresize = function () {
- if (h != window.innerHeight || w != window.innerWidth) {
- window.close();
- window.location = "about:blank";
- }
- }
- </script>
复制代码 更多禁用js- //禁止鼠标右击
- 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();
- }
复制代码 |