限制右键

  1. function oncontextmenu(event) {
  2. console.log(event)
  3. var e = event || window.event || arguments.callee.caller.arguments[0];
  4. if (e.button == 2) {
  5. return false;
  6. }
  7. }

限制按键按下

  1. function onkeydown(e) {
  2. var currKey = 0
  3. var evt = e || window.event;
  4. currKey = evt.keyCode || evt.which || evt.charCode;
  5. if (e.shiftKey && (currKey === 73 || currKey === 74)) {
  6. return false
  7. } else if (currKey == 123) {
  8. return false
  9. }
  10. }

通过比对页面和浏览器的宽高来动态处理body中的内容

  1. // 请将脚本放置到body末尾使用,保证控制台被无故开启后脚本泄漏的风险
  2. var isOpenConsole = false
  3. var current = window.location.href;
  4. (function () {
  5. // 是否有解除限制的标识,解除请在href后增加'?nolimit'
  6. if (current.indexOf('nolimit') === -1) {
  7. console.info('启用浏览器调试限制:\n 1.限制右键\n 2.限制F12\n 3.限制保存图片\n 4.限制复制\n 5.限制选择')
  8. // 限制: 右键
  9. document.oncontextmenu = oncontextmenu;
  10. // 限制: f12
  11. document.onkeydown = onkeydown;
  12. // 限制: 浏览器通过设置开启开发模式
  13. isConsole();
  14. window.onresize = isConsole;
  15. // 限制: 保存,拖拽图片
  16. for (i in document.images) {
  17. document.images[i].ondragstart = function () { return false; };
  18. }
  19. // 限制: 复制
  20. document.body.oncopy = function () {
  21. return false;
  22. }
  23. // 限制: 选择
  24. document.onselectstart = function () {
  25. return false;
  26. }
  27. // 恢复: 已经开启控制台后已经将内容修改,监听关闭控制台的快捷键被抬起后刷新页面
  28. document.onkeyup = onkeyup
  29. }
  30. })()

恢复后刷新页面

  1. function onkeyup(e) {
  2. var currKey = 0
  3. var evt = e || window.event;
  4. currKey = evt.keyCode || evt.which || evt.charCode;
  5. if (e.shiftKey && (currKey === 73 || currKey === 74)) {
  6. location.reload()
  7. return false
  8. } else if (currKey == 123) {
  9. location.reload()
  10. return false
  11. }
  12. }

添加移出限制的控制

  1. // 请将脚本放置到body末尾使用,保证控制台被无故开启后脚本泄漏的风险
  2. var isOpenConsole = false
  3. var current = window.location.href;
  4. (function () {
  5. // 是否有解除限制的标识,解除请在href后增加'?remove-limit'
  6. if (current.indexOf('remove-limit') === -1) {
  7. console.info('启用浏览器调试限制:\n 1.限制右键\n 2.限制F12\n 3.限制保存图片\n 4.限制复制\n 5.限制选择')
  8. // 限制: 右键
  9. document.oncontextmenu = oncontextmenu;
  10. // 限制: f12
  11. document.onkeydown = onkeydown;
  12. // 限制: 浏览器通过设置开启开发模式
  13. isConsole();
  14. window.onresize = isConsole;
  15. // 限制: 保存,拖拽图片
  16. for (i in document.images) {
  17. document.images[i].ondragstart = function () { return false; };
  18. }
  19. // 限制: 复制
  20. document.body.oncopy = function () {
  21. return false;
  22. }
  23. // 限制: 选择
  24. document.onselectstart = function () {
  25. return false;
  26. }
  27. // 恢复: 已经开启控制台后已经将内容修改,监听关闭控制台的快捷键被抬起后刷新页面
  28. document.onkeyup = onkeyup
  29. }
  30. })()