Chrome禁用开发者工具

在一次工作中,所做的项目要求页面中不能右击,不能打开F12。一般来说可以禁用F12的按键,但是可以通过开发者工具进入。经过个人实验,以下方法适用于谷歌浏览器、火狐浏览器,以及使用谷歌内核的浏览器(如QQ浏览器、搜狗浏览器等),IE忘了是否支持,自我感觉是目前比较齐全的了。

  1. //禁止鼠标右击
  2. document.oncontextmenu = function() {
  3. event.returnValue = false;
  4. };
  5. //禁用开发者工具F12
  6. document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
  7. let e = event || window.event || arguments.callee.caller.arguments[0];
  8. if (e && e.keyCode == 123) {
  9. e.returnValue = false;
  10. return false;
  11. }
  12. };
  13. let userAgent = navigator.userAgent;
  14. if (userAgent.indexOf("Firefox") > -1) {
  15. let checkStatus;
  16. let devtools = /./;
  17. devtools.toString = function() {
  18. checkStatus = "on";
  19. };
  20. setInterval(function() {
  21. checkStatus = "off";
  22. console.log(devtools);
  23. console.log(checkStatus);
  24. console.clear();
  25. if (checkStatus === "on") {
  26. let target = "";
  27. try {
  28. window.open("about:blank", (target = "_self"));
  29. } catch (err) {
  30. let a = document.createElement("button");
  31. a.onclick = function() {
  32. window.open("about:blank", (target = "_self"));
  33. };
  34. a.click();
  35. }
  36. }
  37. }, 200);
  38. } else {
  39. //禁用控制台
  40. let ConsoleManager = {
  41. onOpen: function() {
  42. alert("Console is opened");
  43. },
  44. onClose: function() {
  45. alert("Console is closed");
  46. },
  47. init: function() {
  48. let self = this;
  49. let x = document.createElement("div");
  50. let isOpening = false,
  51. isOpened = false;
  52. Object.defineProperty(x, "id", {
  53. get: function() {
  54. if (!isOpening) {
  55. self.onOpen();
  56. isOpening = true;
  57. }
  58. isOpened = true;
  59. return true;
  60. }
  61. });
  62. setInterval(function() {
  63. isOpened = false;
  64. console.info(x);
  65. console.clear();
  66. if (!isOpened && isOpening) {
  67. self.onClose();
  68. isOpening = false;
  69. }
  70. }, 200);
  71. }
  72. };
  73. ConsoleManager.onOpen = function() {
  74. //打开控制台,跳转
  75. let target = "";
  76. try {
  77. window.open("about:blank", (target = "_self"));
  78. } catch (err) {
  79. let a = document.createElement("button");
  80. a.onclick = function() {
  81. window.open("about:blank", (target = "_self"));
  82. };
  83. a.click();
  84. }
  85. };
  86. ConsoleManager.onClose = function() {
  87. alert("Console is closed!!!!!");
  88. };
  89. ConsoleManager.init();
  90. }
上一篇:汽车气缸盖的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告


下一篇:前台技术学习3