Javascript检测开发者模式Devtools是否打开
最近看到蛮多站都加上了反调试,之前也有了解过这方面的东西,但那些反调试代码基本上准确率都不高而且兼容性不太好,现整理出了到目前为止准确率能达到100%,chrome、firefox、ie全兼容的一段代码:
<script type="text/javascript">
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';
},
offed: function() {
document.body.innerHTML = 'Dev Tools is off';
}
});
</script>