侧边栏壁纸
博主头像
菠萝饭博主等级

懒人一枚

  • 累计撰写 29 篇文章
  • 累计创建 3 个标签
  • 累计收到 7 条评论

禁止f12及浏览器右键查看.md

菠萝饭
2021-12-13 / 0 评论 / 0 点赞 / 115 阅读 / 2,131 字

前言

今天在一个网站看美剧时突然想下载下来,就想f12查看视频链接后下载。但发现每次f12或右键检查时都会跳转到一个提示页面,不让查看在线播放页面的控制台。觉得十分有用,就自己研究实现了下:

禁止键盘f12打开控制台:
// 按键事件
document.onkeydown = keyPress;
function keyPress(event) {
    event = event ? event : window.event;
    var keyCode = event.which ? event.which : event.keyCode;
    if(keyCode == 123){//禁止键盘f12按键
        window.event.cancelBubble = true;
        window.event.returnValue = false;
        //也可以在按f12后跳转到指定提示页面
    }
}

上步只禁止了键盘点击f12,对鼠标右键检查打开控制台没有措施

开启监听事件,在检查控制台打开后跳转提示页
//检测控制台是否打开
(function () {
    'use strict';
    var devtools = {
        open: false,
        orientation: null
    };
    var threshold = 160;
    var emitEvent = function (state, orientation) {
        window.dispatchEvent(new CustomEvent('devtoolschange', {
            detail: {
                open: state,
                orientation: orientation
            }}
        ))
    };
    setInterval(function () {
        var widthThreshold = window.outerWidth - window.innerWidth > threshold;
        var heightThreshold = window.outerHeight - window.innerHeight > threshold;
        var orientation = widthThreshold ? 'vertical' : 'horizontal';

        if (!(heightThreshold && widthThreshold) &&
        ((window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized) || widthThreshold || heightThreshold)) {
            if (!devtools.open || devtools.orientation !== orientation) {
                emitEvent(true, orientation)
            }
            devtools.open = true;
            devtools.orientation = orientation
        } else {
            if (devtools.open) {
                emitEvent(false, null)
            }
            devtools.open = false;
            devtools.orientation = null
        }
    }, 500);
    if (typeof module !== 'undefined' && module.exports) {
        module.exports = devtools
    } else {
        window.devtools = devtools
    }
})();

//开启监视器
window.addEventListener('devtoolschange', function (e) {
    if (e.detail.open) console.clear();
    console.log('*******************')
    //跳转到错误页
    window.location.href="correct.jsp";
});
0

评论区