Skip to content
防爬虫js代码
js
<script>
    // 1. 禁用右键
    document.addEventListener('contextmenu', e => e.preventDefault());

    // 禁用复制
    document.addEventListener('copy', e => e.preventDefault());

    // 2. 禁用 F12 / Ctrl+U / Ctrl+C
    document.onkeydown = e => {
      if (e.key === 'F12') return false; // 禁用 F12
      if (e.ctrlKey && e.key === 'u') return false; // 禁用 Ctrl+U
      if (e.ctrlKey && e.key === 'c') return false; // 禁用 Ctrl+C
      if (e.ctrlKey && e.key === '83') return false; // 禁用 Ctrl+S
      if (e.key == '122' || e.key == '123') return false; // 禁用 F11 / F12
    };

    // 3. 打开 DevTools 一次陷阱(不循环),无限bebugger
    !function test() {
      // 捕获异常,递归次数过多调试工具会抛出异常。
      try {
        !
        function cir(i) {
          // 当打开调试工具后,抛出异常,setTimeout执行test无参数,此时i == NaN,("" + i / i).length == 3
          // debugger设置断点
          (1 !== ("" + i / i).length || 0 === i) &&
            function () { }.constructor("debugger")(),
            cir(++i);
        }(0)
      } catch (e) {
        setTimeout(test, 500)
      }
    }()
  </script>