转载自此处
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';
}
});
通过此代码可以监听到是否打开开发者调试窗口。如果打开那么可以移除一些敏感信息,或者防爬虫设定。
后续,又有一种兼容性较好的版本
setInterval(function() {
check()
}, 4000);
var check = function() {
function doCheck(a) {
if (("" + a / a)["length"] !== 1 || a % 20 === 0) {
(function() {}
["constructor"]("debugger")())
} else {
(function() {}
["constructor"]("debugger")())
}
doCheck(++a)
}
try {
doCheck(0)
} catch (err) {}
};
check();