一个简单的 F12 检测脚本
js
// 控制台检测
(function(){
let devtools = {
isOpen: false,
orientation: undefined,
};
let threshold = 170;
let emitEvent = function(isOpen, orientation){
globalThis.dispatchEvent(new globalThis.CustomEvent('devtoolschange', {
detail: {
isOpen,
orientation,
}
}));
};
let main = function(emitEvents){
let widthThreshold = globalThis.outerWidth - globalThis.innerWidth > threshold;
let heightThreshold = globalThis.outerHeight - globalThis.innerHeight > threshold;
let orientation = widthThreshold ? 'vertical' : 'horizontal';
if (
!(heightThreshold && widthThreshold)
&& ((globalThis.Firebug && globalThis.Firebug.chrome && globalThis.Firebug.chrome.isInitialized) || widthThreshold || heightThreshold)
) {
if ((!devtools.isOpen || devtools.orientation !== orientation) && emitEvents) {
emitEvent(true, orientation);
}
devtools.isOpen = true;
devtools.orientation = orientation;
} else {
if (devtools.isOpen && emitEvents) {
emitEvent(false, undefined);
}
devtools.isOpen = false;
devtools.orientation = undefined;
}
};
main({emitEvents: false});
setInterval(function(){
main({emitEvents: true});
}, 500);
let showWarning = function(){
document.body.innerHTML = '<p style="padding:25px 20px;font-size:48px;color:red;">开发不易,抄袭可耻!</p>\
<p style="padding:0 20px;font-size:20px;">互联网是开放的,但内容创造凝聚着开发者的心血。<br/>前端或许没有秘密,设置障碍纯粹是作为开发者保护自己作品最后的一点倔强!<br/>但愿你在喜欢我们作品的同时能善待它~<br/>如若有缘,江湖再见!</p>';
};
if(devtools.isOpen){
showWarning();
}
window.addEventListener('devtoolschange', function(event){
if(event.detail.isOpen){
showWarning();
}else{
location.reload();
}
});
})();