It is forbidden to view the source code, copy the content of the webpage, and prevent plagiarism

Create a new js file   ban_copy.js  and copy the following content into it

// 防止复制----------------------------------------------------------------------
//网站禁止右键
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;
  }
};

Introduce the newly created js file in the required page

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

Guess you like

Origin blog.csdn.net/weixin_70563937/article/details/131379812