浅生 发表于 2020-7-8 10:20:00

防止网页被审查元素JS代码

防止网页被审查元素JS代码,可以防止一般用户在网页上审查元素
<script type="text/javascript">window.onkeydown = window.onkeyup = window.onkeypress = function (event) {      // 判断是否按下F12,F12键码为123      if (event.keyCode == 123) {          event.preventDefault(); // 阻止默认事件行为          window.event.returnValue = false;      }}   var threshold = 160; // 打开控制台的宽或高阈值// 每秒检查一次window.setInterval(function() {      if (window.outerWidth - window.innerWidth > threshold ||       window.outerHeight - window.innerHeight > threshold) {          // 如果打开控制台,则刷新页面          window.location.reload();      }}, 1e3);</script><script type="text/javascript">function click(e) {if (document.all) {if (event.button==2||event.button==3) { alert("DZ插件网禁止操作");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); } }</script>

Discuz智能体 发表于 2025-3-19 02:01:49

在Discuz论坛中,如果你想防止用户通过浏览器的开发者工具(如F12)来审查网页元素,可以使用一些JavaScript代码来实现。以下是一个示例代码,它可以阻止用户按下F12键打开开发者工具,并且当用户尝试打开控制台时,页面会自动刷新。

// 阻止F12键打开开发者工具
window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
    // 判断是否按下F12,F12键码为123
    if (event.keyCode == 123) {
      event.preventDefault(); // 阻止默认事件行为
      window.event.returnValue = false;
    }
}

// 检测控制台是否打开
var threshold = 160; // 打开控制台的宽或高阈值
window.setInterval(function() {
    if (window.outerWidth - window.innerWidth > threshold ||
    window.outerHeight - window.innerHeight > threshold) {
      // 如果打开控制台,则刷新页面
      window.location.reload();
    }
}, 1000); // 每秒检查一次

// 禁用右键菜单
function click(e) {
    if (document.all) {
      if (event.button == 2 || event.button == 3) {
            alert("DZ插件网禁止操作");
            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;");

// 阻止F12键打开开发者工具
document.onkeydown = document.onkeyup = document.onkeypress = function() {
    if (window.event.keyCode == 123) {
      window.event.returnValue = false;
      return false;
    }
}

### 代码说明:
1. **阻止F12键**:通过监听键盘事件,当用户按下F12键时,阻止默认行为并返回`false`,从而阻止开发者工具打开。
2. **检测控制台**:通过比较`window.outerWidth`和`window.innerWidth`的差值,或者`window.outerHeight`和`window.innerHeight`的差值,来判断用户是否打开了控制台。如果差值超过设定的阈值,页面会自动刷新。
3. **禁用右键菜单**:通过监听鼠标右键点击事件,阻止用户通过右键菜单打开开发者工具。

### 注意事项:
- 这种方法并不能完全阻止用户审查元素,因为用户可以通过其他方式(如浏览器设置、插件等)绕过这些限制。
- 这种代码可能会影响用户体验,特别是在用户需要使用右键菜单或开发者工具进行调试时。
- 如果你需要更高级的保护措施,建议结合服务器端的验证和加密技术来保护你的网页内容。

如果你有更多关于Discuz开发的问题,欢迎随时提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
页: [1]
查看完整版本: 防止网页被审查元素JS代码