前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >禁用浏览器右键菜单等操作

禁用浏览器右键菜单等操作

作者头像
薛定喵君
发布2019-11-05 19:38:28
2.8K0
发布2019-11-05 19:38:28
举报
文章被收录于专栏:薛定喵君薛定喵君

防止用户在浏览器中打开右键菜单、文本选中、复制内容、F12打开控制台的操作

右键菜单

代码语言:javascript
复制
document.oncontextmenu = function (event) {
  event.preventDefault();
};

文本选中

代码语言:javascript
复制
if(document.all){
   document.onselectstart= function(){return false;}; //for ie
}else{
   document.onmousedown= function(){return false;};
   document.onmouseup= function(){return true;};
}
document.onselectstart = new Function('event.returnValue=false;');
代码语言:javascript
复制
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
-khtml-user-select: none;
user-select: none;

复制内容

代码语言:javascript
复制
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;
   }
}

F12打开控制台

代码语言:javascript
复制
document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
    var e = event || window.event || arguments.callee.caller.arguments[0];

    if (e && e.keyCode == 123) {
            e.returnValue = false;
            return (false);
    }
}

检测用户打开控制台后强制跳转

代码语言:javascript
复制
var ConsoleManager = {
 onOpen: function() {
   alert('Console is opened');
 },
 onClose: function() {
   alert('Console is closed');
 },
 init: function() {
   var self = this;
   var x = document.createElement('div');
   var isOpening = false,
     isOpened = false;
   Object.defineProperty(x, 'id', {
     get: function() {
       if (!isOpening) {
         self.onOpen();
         isOpening = true;
       }
       isOpened = true;
     }
   });
   setInterval(function() {
     isOpened = false;
     console.info(x);
     console.clear();
     if (!isOpened && isOpening) {
       self.onClose();
       isOpening = false;
     }
   }, 200);
 }
};

ConsoleManager.onOpen = function() {
 //打开控制台,跳转到百度
 try {
   window.open('https://www.baidu.com/', (target = '_self'));
 } catch (err) {
   var a = document.createElement('button');
   a.onclick = function() {
     window.open('https://www.baidu.com', (target = '_self'));
   };
   a.click();
 }
};
ConsoleManager.onClose = function() {
 alert('Console is closed!!!!!');
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 右键菜单
  • 文本选中
  • 复制内容
  • F12打开控制台
  • 检测用户打开控制台后强制跳转
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档