首页 知识分享内容详情

教你灵活解除网页操作限制

2022-01-29 309 admin

前言下午帮客户分析某文学登陆业务中,发现有页面禁用了网页右键,非常影响调试,平时遇到这种情况通常都是JS即可,但是网上查阅了资料后发现用控制台调节更灵活一些,毕竟禁用 JS 可能出现一些错位现象...


实现禁止操作

既然我们要解除就要先看看禁止效果是如何失效的,以下代码放入网站JS里面引用即可实现效果。

// 禁止右键菜单
document.oncontextmenu = function(){ return false; };
document.oncontextmenu= new Function("event.returnValue=false");
// 禁止文字选择
document.onselectstart = function(){ return false; };
document.onselectstart = new Function("event.returnValue=false");
// 禁止复制
document.oncopy = function(){ return false; };
document.oncopy = new Function("event.returnValue=false");
// 禁止剪切
document.oncut = function(){ return false; };
document.oncopy = new Function("event.returnValue=false");
// 禁止粘贴
document.onpaste = function(){ return false; };
document.onpaste = new Function("event.returnValue=false");
// 禁止F12
document.onkeydown = function () {
    if (window.event && window.event.keyCode == 123) {
        event.keyCode = 0;
        event.returnValue = true;
        return true;
    }
};


解除禁止操作

通常直接按F12,如果此键被禁止可以通过SHIFT + CTRL + I打开,或者通过浏览器菜单里面的“开发人员工具”
选择控制台,输入以下代码回车即可。

// 开启右键菜单
document.oncontextmenu = function(){ return true; };
// 开启文字选择
document.onselectstart = function(){ return true; };
// 开启复制
document.oncopy = function(){ return true; };
// 开启剪切
document.oncut = function(){ return true; };
// 开启粘贴
document.onpaste = function(){ return true; };
// 开启F12键
document.onkeydown = function () {
    if (window.event && window.event.keyCode == 123) {
        event.keyCode = 0;
        event.returnValue = true;
        return true;
    }
};


相关标签: # 网页 # 限制 # 右键 # 复制 # 粘贴 # 剪切

 暂无评论,快来抢沙发吧~

发布评论