index.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <div style="display: none">禁用F12/右键事件启用中</div>
  3. </template>
  4. <script>
  5. import { addListener, launch } from 'devtools-detector';
  6. export default {
  7. name: 'EventControl',
  8. data() {
  9. return {
  10. threshold: 300,
  11. checkTimer: null,
  12. keyListener: function(e) {
  13. if (e.code === 'F12' ||
  14. e.code === 'MetaLeft' ||
  15. e.code === 'MetaRight' ||
  16. e.code === 'AltLeft' ||
  17. e.code === 'AltRight' ||
  18. e.code === 'ControlLeft' ||
  19. e.code === 'ControlRight') {
  20. // 阻止
  21. e.stopPropagation()
  22. e.preventDefault()
  23. }
  24. }
  25. }
  26. },
  27. mounted() {
  28. // 禁止复制
  29. this.$nextTick(() => {
  30. // 禁用右键
  31. document.oncontextmenu = function() {
  32. return false
  33. }
  34. })
  35. // 禁用F12
  36. document.addEventListener('keydown', this.keyListener)
  37. // // 检查是否打开控制台
  38. // this.checkTimer = setInterval(() => {
  39. // if (window.outerWidth - window.innerWidth > this.threshold ||
  40. // window.outerHeight - window.innerHeight > this.threshold) {
  41. // // 如果打开控制台,则刷新页面
  42. // // window.location.reload()
  43. // this.$message.warning('不允许使用调试模式,系统将在3秒后跳转到首页!')
  44. // setTimeout(() => {
  45. // window.location.href = '/'
  46. // }, 3000)
  47. // }
  48. // }, 3000)
  49. // 1. add listener
  50. addListener(
  51. isOpen => {
  52. if (isOpen) {
  53. this.$message.warning('不允许使用调试模式,系统将在3秒后跳转到首页!')
  54. setTimeout(() => {
  55. window.location.href = '/'
  56. }, 3000)
  57. }
  58. }
  59. )
  60. // 2. launch detect
  61. launch()
  62. },
  63. beforeDestroy() {
  64. // 还原右键
  65. document.oncontextmenu = function() {
  66. return true
  67. }
  68. // 还原F12
  69. document.removeEventListener('keydown', this.keyListener)
  70. // 清除定时器
  71. clearInterval(this.checkTimer)
  72. }
  73. }
  74. </script>