禁止查看源代码复制网页内容防止扒站

新建一个js文件  ban_copy.js 复制以下内容进去

// 防止复制----------------------------------------------------------------------
//网站禁止右键
document.body.oncontextmenu = function () {
  self.event.returnValue = false;
};
//网站禁止选择文字
document.body.onselectstart = function () {
  self.event.returnValue = false;
};
//禁用F12 和 ctrl+u 和保存
window.onkeydown =
  window.onkeyup =
  window.onkeypress =
    function (event) {
      // 判断是否按下F12,F12键码为123 u为85 s为83
      if (
        event.keyCode == 123 ||
        (event.ctrlKey && event.keyCode == 85) ||
        (event.ctrlKey && event.keyCode == 83)
      ) {
        event.preventDefault(); // 阻止默认事件行为
        window.event.returnValue = false;
      }
    };
//禁用调试工具
var threshold = 160; // 打开控制台的宽或高阈值
// 每秒检查一次
var check = setInterval(function () {
  if (
    window.outerWidth - window.innerWidth > threshold ||
    window.outerHeight - window.innerHeight > threshold
  ) {
    // 如果打开控制台,则刷新页面
    window.location.reload();
  }
}, 1000);
//屏蔽复制
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;
  }
};
//屏蔽剪贴
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;
  }
};
//屏蔽粘贴
document.onpaste = 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;
  }
};

在需要的页面引入新建的js文件就可以了

<script src="static/js/ban_copy.js"></script>

猜你喜欢

转载自blog.csdn.net/weixin_70563937/article/details/131379812
今日推荐