Disable web developer tools from the f12 button, right-click and browser menu
by Good Muyis
HTML
<input type="text" value="hello">
lorem ipsum
JavaScript
(function() {
// Configuration object
const config = {
disableContextMenu: true,
disableShortcuts: true,
disableSelection: true,
};
// Disable right-click context menu
if (config.disableContextMenu) {
document.addEventListener("contextmenu", function(e) {
e.preventDefault();
});
}
// Disable shortcuts like F12 and Ctrl+Shift+I
if (config.disableShortcuts) {
document.addEventListener("keydown", function(e) {
if (e.key === "F12" || (e.ctrlKey && e.shiftKey && e.key === "I")) {
e.preventDefault();
console.log("Developer tools might be open.");
}
});
}
// Recognize developer tools opening from browser menu bar
window.addEventListener("resize", function() {
if (window.outerWidth - window.innerWidth > 100 || window.outerHeight - window.innerHeight > 100) {
console.log("Developer tools might be open.");
}
});
// Close current page
function closePage() {
window.open("", "_self");
window.close();
}
// Identify real mobile terminal
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
// Identify developer tools close events
window.addEventListener("beforeunload", function(e) {
const confirmationMessage = "Are you sure you want to leave?";
e.returnValue = confirmationMessage;
return confirmationMessage;
});
// Disable selection, copy, cut, paste functions
/* if (config.disableSelection) {
document.addEventListener("selectstart", function(e) { e.preventDefault(); });
document.addEventListener("copy", function(e) {e.preventDefault();});
document.addEventListener("cut", function(e) {e.preventDefault();});
document.addEventListener("paste", function(e) {e.preventDefault();});
} */
// Expose the library as a global object
window.MyCustomLibrary = {
closePage: closePage,
decryptParams: decryptParams,
isMobile: isMobile,
// Add more functions...