Skip to content

Instantly share code, notes, and snippets.

@inter-coder
Last active December 23, 2022 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inter-coder/07632529b7400fb55a890cc085810dfd to your computer and use it in GitHub Desktop.
Save inter-coder/07632529b7400fb55a890cc085810dfd to your computer and use it in GitHub Desktop.
Clearing the console alone is not enough, and a better approach is to disable the developer tool from being opened in any meaningful way
(function() {
'use strict';
Object.getOwnPropertyNames(console).filter(function(property) {
return typeof console[property] == 'function';
}).forEach(function (verb) {
console[verb] =function(){return 'Sorry, for security reasons...';};
});
window.addEventListener('devtools-opened', ()=>{
// do some extra code if needed or ...
// maybe even delete the page, I still like to add redirect just in case
/*window.location.href+="#";
window.document.head.innerHTML="";
window.document.body.innerHTML="devtools, page is now cleared";*/
for (let variable in window) {
if (window.hasOwnProperty(variable)) {
try {
window[variable]="devtools_detected";
} catch (e) {
console.error(e);
}
}
};
});
window.addEventListener('devtools-closed', ()=>{
// do some extra code if needed
});
let verifyConsole = () => {
var before = new Date().getTime();
debugger;
var after = new Date().getTime();
if (after - before > 100) { // user had to resume the script manually via opened dev tools
window.dispatchEvent(new Event('devtools-opened'));
}else{
window.dispatchEvent(new Event('devtools-closed'));
}
setTimeout(verifyConsole, 100);
}
verifyConsole();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment