Created
July 6, 2021 08:50
-
-
Save drinkmaker/67c5dd6c83170a65517a595a5fa135d8 to your computer and use it in GitHub Desktop.
Developer Tools Detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Disable Developer Tool, HotKeys: Ctrl+Shift+I, ⌘+⌥+I, F12 | |
* Disable to View Source Code of Page by Developer Tools. | |
**/ | |
function disableDeveloperTool() { | |
hotkeys( 'command+option+j,command+option+i,command+shift+c,command+option+c,command+option+k,command+option+z,command+option+e,f12,ctrl+shift+i,ctrl+shift+j,ctrl+shift+c,ctrl+shift+k,ctrl+shift+e,shift+f7,shift+f5,shift+f9,shift+f12', function( event, handler ) { | |
event.preventDefault(); | |
} ); | |
(function () { | |
if ( typeof window.CustomEvent === "function" ) return false; | |
function CustomEvent( event, params ) { | |
params = params || { bubbles: false, cancelable: false, detail: undefined }; | |
var evt = document.createEvent( "CustomEvent" ); | |
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); | |
return evt | |
} | |
CustomEvent.prototype = window.Event.prototype; | |
window.CustomEvent = CustomEvent | |
})(); | |
(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; | |
} | |
})(); | |
let element = new Image; | |
let ua = navigator.userAgent.toLowerCase(); | |
let msie = ua.indexOf( "msie" ) > -1 || ua.indexOf( "edge/" ) > -1; | |
if ( ! msie && navigator.appName === "Netscape" && ua.indexOf( "trident/" ) > -1 ) { msie = true; } | |
var chrome = ! msie && ua.indexOf( "chrome" ) > -1; | |
var safari = ! chrome && ! msie && ua.indexOf( "safari" ) > -1; | |
let consoleMsg = "No source code for you! Close the console to refresh the page :)"; | |
let consoleStyle = "background: black; color: transparent; font-size: 0px;"; | |
let deb = 0; | |
let timerMax = 500; | |
let utm = '__utmq'; | |
/** Renamed console.log to soleCon.go */ | |
let soleCon = clone( console ); | |
/** Copy and rename console.log to soleCon.go */ | |
function clone( obj ) { | |
if ( null == obj || "object" != typeof obj ) { return obj; } | |
let copy = obj.constructor(); | |
for ( let attr in obj ) { | |
if ( obj.hasOwnProperty( attr ) ) { | |
copy[attr === "log" ? "go" : attr] = obj[attr]; | |
} | |
} | |
return copy; | |
} | |
function opened() { | |
rmbody(); | |
} | |
function closed() { | |
noconsole(); | |
location.reload(); | |
} | |
function noconsole2() { | |
noconsole(); | |
} | |
function noconsole() { | |
if ( readCookie( utm ) === '1' ) { return; } | |
createCookie( utm, 1, 365 * 10 ); | |
if ( inIframe() ) { return; } | |
location.reload(); | |
} | |
function inIframe () { | |
try { | |
return window.self !== window.top; | |
} catch (e) { | |
return true; | |
} | |
} | |
function egg8log( arg ) { | |
if ( msie ) { | |
arg ? soleCon.go( consoleMsg, arg ) : soleCon.go( consoleMsg ); | |
} else { | |
arg ? soleCon.go( "%c" + consoleMsg, consoleStyle, arg ) : soleCon.go( "%c" + consoleMsg, consoleStyle ) | |
} | |
} | |
if ( readCookie( utm ) !== '1' ) { | |
let noconyet = 0; | |
let threshold = 160; | |
let widthThreshold = window.outerWidth - window.innerWidth > threshold; | |
let heightThreshold = window.outerHeight - window.innerHeight > threshold; | |
let orientation = widthThreshold ? "vertical" : "horizontal"; | |
if ( !(heightThreshold && widthThreshold) && | |
(window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized || widthThreshold || heightThreshold) ) { | |
if ( ! devtools.open || devtools.orientation !== orientation ) {} | |
} else { | |
noconsole(); | |
noconyet = 1; | |
} | |
} | |
dtToStr(); | |
element.__defineGetter__( 'id', function () { | |
if ( intr ) { clearTimeout( intr ); } | |
rmbody(); | |
setTimeout( noconsole2, timerMax * 1.5 ); | |
} ); | |
window.addEventListener( 'devtoolschange', function ( e ) { | |
if ( e.detail.open ) { | |
rmbody(); | |
egg8log(); | |
} else { | |
noconsole(); | |
} | |
} ); | |
/** Remove Document body on devtools open. */ | |
function rmbody() { | |
if ( document.body != null ) { | |
document.body.parentNode.removeChild( document.body ); | |
} | |
if ( document.head != null ) { | |
document.head.parentNode.removeChild( document.head ); | |
} | |
} | |
function dtToStr() { | |
let cnt = 0; | |
let open = 0; | |
if ( ! safari && ! chrome ) { return; } | |
let isOpen = () => cnt > (safari ? 0 : chrome ? 1 : 1); | |
let w = new Function; | |
w.toString = () => { | |
cnt++; | |
checkOpen() | |
}; | |
let test = () => { | |
checkClosed(); | |
cnt = 0; | |
egg8log( w ); | |
}; | |
let checkOpen = () => { | |
if ( isOpen() && ! open ) { | |
open = 1; | |
opened(); | |
} | |
}; | |
let checkClosed = () => { | |
if ( ! isOpen() && open ) { | |
open = 0; | |
closed(); | |
} | |
}; | |
setInterval( test, 500 ); | |
} | |
function createCookie( name, value, days ) { | |
if ( days > 0 ) { eraseCookie( name ); } | |
let expires = ''; | |
if ( days ) { | |
let date = new Date; | |
date.setTime( date.getTime() + days * 24 * 60 * 60 * 1000 ); | |
expires = "; expires=" + date.toGMTString(); | |
} | |
document.cookie = name + "=" + value + expires + "; path=/" | |
} | |
function readCookie( name ) { | |
let nameEQ = name + "="; | |
let ca = document.cookie.split( ";" ); | |
for ( let i = 0; i < ca.length; i++ ) { | |
let c = ca[i]; | |
while ( c.charAt( 0 ) === ' ' ) { | |
c = c.substring( 1, c.length ); | |
} | |
if ( c.indexOf( nameEQ ) === 0 ) { | |
return c.substring( nameEQ.length, c.length ); | |
} | |
} | |
return null | |
} | |
function eraseCookie( name ) { | |
createCookie( name, '', -1 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment