禁止開發者工具

lv_longcheng發表於2020-09-28

 

// 禁止按鈕F12
document.onkeydown = function (event) {
    var ev = event || window.event || arguments.callee.caller.arguments[0];
    console.log(event.keyCode)
    if (event.keyCode === 123) {
        return false; //123 代表F12鍵位
    }
}
// 禁止右鍵
document.oncontextmenu = function (e) {
    e.preventDefault();
};

function checkDevTools(options) {
    const isFF = ~navigator.userAgent.indexOf("Firefox");
    let toTest = '';
    if (isFF) {
        toTest = /./;
        toTest.toString = function () {
            options.opened();
        }
    } else {
        toTest = new Image();
        toTest.__defineGetter__('id', function () {
            options.opened();
        });
    }
    setInterval(function () {
        options.offed();
        console.log(toTest);
        console.clear && console.clear();
    }, 1000);
}

checkDevTools({
    opened: function () {
        // document.body.innerHTML = 'Dev Tools is on';
        window.location.href = 'about:blank'
    },
    offed: function () {
        // document.body.innerHTML = 'Dev Tools is off';
    }
});

 

相關文章